Skip to content

Commit 9798694

Browse files
committed
feat: plugins API
- Also add utilities: animationDuration, animationTimingFunction, maskSize. - Add a new option "multiple" on `style` for comma separated values. "shorthand" remain for space separated values. - Add "cssProps" option on `style`. Useful to specify which css properties must be interpolated when using `css` or `styled`. - Introduce `createCss` function to create a complete set of binded xstyled functions. BREAKING CHANGE: - Remove `x.extend` - Remove `createX` - TypeScript: use `Theme` from `@xstyled/system` everywhere.
1 parent fbbf91e commit 9798694

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+657
-737
lines changed

packages/core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from './breakpoints'
22
export * from './colorModes'
33
export * from './createBox'
4-
export * from './propGetters'
54
export * from './transform'
65
export * from './theme'
76
export * from './types'

packages/core/src/propGetters.test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/core/src/propGetters.ts

Lines changed: 0 additions & 181 deletions
This file was deleted.

packages/core/src/transform.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { transform } from './transform'
1+
import { system } from '@xstyled/system'
2+
import { createTransform } from './transform'
23

34
const props = {
45
theme: {
@@ -7,6 +8,8 @@ const props = {
78
},
89
}
910

11+
const transform = createTransform(system)
12+
1013
const transformToStr = (s: string) =>
1114
transform(s)
1215
.map((x: Function | string) => (typeof x === 'function' ? x(props) : x))

packages/core/src/transform.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-continue, no-loop-func, no-cond-assign */
2+
import type { StyleGenerator } from '@xstyled/system'
23
import { mediaGetters } from './mediaGetters'
3-
import { propGetters } from './propGetters'
44

55
// prop name is an ident: word chars, underscore and dash.
66
const PROP_CHAR = `[-\\w]`
@@ -62,31 +62,33 @@ const mediaTransform = (rawValue: string) => {
6262
return values
6363
}
6464

65-
export const transform = (rawValue: any): any => {
66-
if (typeof rawValue !== 'string') return rawValue
67-
let matches
68-
let lastIndex = 0
69-
const values = []
70-
while ((matches = MATCH_REGEXP.exec(rawValue))) {
71-
const [, prop, colon, value, imp, semi, media, query, brace] = matches
72-
if (media) {
73-
values.push(rawValue.slice(lastIndex, matches.index))
74-
values.push(media)
75-
mediaTransform(query).forEach((v) => values.push(v))
76-
values.push(brace)
77-
lastIndex = matches.index + matches[0].length
78-
} else {
79-
const getter = (propGetters as any)[prop]
80-
if (getter) {
65+
export const createTransform =
66+
(generator: StyleGenerator) =>
67+
(rawValue: any): any => {
68+
if (typeof rawValue !== 'string') return rawValue
69+
let matches
70+
let lastIndex = 0
71+
const values = []
72+
while ((matches = MATCH_REGEXP.exec(rawValue))) {
73+
const [, prop, colon, value, imp, semi, media, query, brace] = matches
74+
if (media) {
8175
values.push(rawValue.slice(lastIndex, matches.index))
82-
values.push(
83-
(p: object) =>
84-
`${prop}${colon}${getter(value)(p)}${imp || ''}${semi}`,
85-
)
76+
values.push(media)
77+
mediaTransform(query).forEach((v) => values.push(v))
78+
values.push(brace)
8679
lastIndex = matches.index + matches[0].length
80+
} else {
81+
const getter = generator.meta.cssGetters[prop]
82+
if (getter) {
83+
values.push(rawValue.slice(lastIndex, matches.index))
84+
values.push(
85+
(p: object) =>
86+
`${prop}${colon}${getter(value)(p)}${imp || ''}${semi}`,
87+
)
88+
lastIndex = matches.index + matches[0].length
89+
}
8790
}
8891
}
92+
values.push(rawValue.slice(lastIndex, rawValue.length))
93+
return values
8994
}
90-
values.push(rawValue.slice(lastIndex, rawValue.length))
91-
return values
92-
}

packages/emotion/src/create.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { StyleGenerator } from '@xstyled/system'
2+
import { createCssFunction, XCSSFunction } from './createCssFunction'
3+
import { createX, X } from './createX'
4+
import { createStyled, XStyled } from './createStyled'
5+
import {
6+
createCreateGlobalStyle,
7+
XCreateGlobalStyle,
8+
} from './createCreateGlobalStyle'
9+
import { createCx, Cx } from './createCx'
10+
import { createJsx, XJsx } from './createJsx'
11+
12+
interface XStyledSet<TGen extends StyleGenerator> {
13+
css: XCSSFunction
14+
x: X<TGen>
15+
styled: XStyled<TGen>
16+
createGlobalStyle: XCreateGlobalStyle
17+
cx: Cx
18+
jsx: XJsx
19+
}
20+
21+
export const createCss = <TGen extends StyleGenerator>(
22+
generator: TGen,
23+
): XStyledSet<TGen> => {
24+
return {
25+
css: createCssFunction(generator),
26+
x: createX(generator),
27+
styled: createStyled(generator),
28+
createGlobalStyle: createCreateGlobalStyle(generator),
29+
cx: createCx(generator),
30+
jsx: createJsx(generator),
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2+
import * as React from 'react'
3+
import { Global, useTheme } from '@emotion/react'
4+
import { StyleGenerator } from '@xstyled/system'
5+
import { createCssFunction, XCSSFunction } from './createCssFunction'
6+
7+
export interface XCreateGlobalStyle<P extends object = {}> {
8+
(...styles: Parameters<XCSSFunction>): React.FC<P>
9+
}
10+
11+
export const createCreateGlobalStyle = <TGen extends StyleGenerator>(
12+
generator: TGen,
13+
): XCreateGlobalStyle => {
14+
const css = createCssFunction(generator)
15+
return <P extends object = {}>(...styles: Parameters<typeof css>) => {
16+
const GlobalStyle = (props: P) => {
17+
const theme = useTheme()
18+
return <Global styles={css(...styles)({ theme, ...props })} />
19+
}
20+
GlobalStyle.displayName = 'GlobalStyle'
21+
return GlobalStyle
22+
}
23+
}

0 commit comments

Comments
 (0)