|
| 1 | +import { StyleGenerator } from '@xstyled/system' |
| 2 | +import { css as scCss } from 'styled-components/native' |
| 3 | +import * as RN from 'react-native' |
| 4 | +import { createCssFunction, XCSSFunction } from '../createCssFunction' |
| 5 | +import { scStyledNative } from './scStyled' |
| 6 | +import { createX } from '../createX' |
| 7 | +import { createStyled, StyledFunctions } from '../createStyled' |
| 8 | +import { X, XStyledNative } from './types' |
| 9 | + |
| 10 | +interface XStyledSet<TGen extends StyleGenerator> { |
| 11 | + css: XCSSFunction |
| 12 | + x: X<TGen> |
| 13 | + styled: XStyledNative<TGen> |
| 14 | +} |
| 15 | + |
| 16 | +const defineStyledInterface = <XObj>({ |
| 17 | + scStyled, |
| 18 | + styled, |
| 19 | + xstyled, |
| 20 | +}: StyledFunctions<XObj>): XObj => { |
| 21 | + Object.keys(scStyled).forEach((tag) => { |
| 22 | + Object.defineProperty(styled, tag, { |
| 23 | + enumerable: true, |
| 24 | + configurable: false, |
| 25 | + get() { |
| 26 | + //@ts-ignore |
| 27 | + return styled(RN[tag]) |
| 28 | + }, |
| 29 | + }) |
| 30 | + |
| 31 | + Object.defineProperty(styled, `${tag}Box`, { |
| 32 | + enumerable: true, |
| 33 | + configurable: false, |
| 34 | + get() { |
| 35 | + //@ts-ignore |
| 36 | + return xstyled(RN[tag]) |
| 37 | + }, |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + return styled as XObj |
| 42 | +} |
| 43 | + |
| 44 | +const defineStyledXInterface = <XObj>({ |
| 45 | + scStyled, |
| 46 | + styled, |
| 47 | + xstyled, |
| 48 | +}: StyledFunctions<XObj>): XObj => { |
| 49 | + Object.keys(scStyled).forEach((tag) => { |
| 50 | + Object.defineProperty(styled, tag, { |
| 51 | + enumerable: true, |
| 52 | + configurable: false, |
| 53 | + get() { |
| 54 | + //@ts-ignore |
| 55 | + return xstyled(RN[tag])`` |
| 56 | + }, |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + return styled as XObj |
| 61 | +} |
| 62 | + |
| 63 | +export const createCss = <TGen extends StyleGenerator>( |
| 64 | + generator: TGen, |
| 65 | +): XStyledSet<TGen> => { |
| 66 | + const css = createCssFunction(scCss, generator) |
| 67 | + |
| 68 | + return { |
| 69 | + css, |
| 70 | + x: defineStyledXInterface(createX(scStyledNative, css, generator)), |
| 71 | + styled: defineStyledInterface(createStyled(scStyledNative, css, generator)), |
| 72 | + } |
| 73 | +} |
0 commit comments