-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathindex.ts
168 lines (145 loc) · 4.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
jsx as emotionJsx,
ThemeContext as EmotionContext,
} from '@emotion/react'
import { Theme } from '@theme-ui/css'
import * as React from 'react'
import deepmerge from 'deepmerge'
import packageInfo from '@emotion/react/package.json'
import { parseProps } from './parseProps'
import { ThemeUIJSX } from './jsx-namespace'
export type { ThemeUIJSX } from './jsx-namespace'
export type {
CSSObject,
CSSOthersObject,
CSSProperties,
CSSPseudoSelectorProps,
ColorMode,
ColorModesScale,
Label,
ResponsiveStyleValue,
Scale,
StylePropertyValue,
TLengthStyledSystem,
Theme,
ThemeDerivedStyles,
ThemeStyles,
ThemeUICSSObject,
ThemeUICSSProperties,
ThemeUIExtendedCSSProperties,
ThemeUIStyleObject,
VariantProperty,
} from '@theme-ui/css'
export * from './types'
const __EMOTION_VERSION__ = packageInfo.version
export const jsx: typeof React.createElement = <P extends {}>(
type: React.FunctionComponent<P> | React.ComponentClass<P> | string,
props: React.Attributes & P,
...children: React.ReactNode[]
): any => emotionJsx(type, parseProps(props), ...children)
/**
* @internal for Babel JSX pragma
* @see https://github.com/system-ui/theme-ui/issues/1603
*/
export const createElement: unknown = jsx
export declare namespace jsx {
export namespace JSX {
export interface Element extends ThemeUIJSX.Element {}
export interface ElementClass extends ThemeUIJSX.ElementClass {}
export interface ElementAttributesProperty
extends ThemeUIJSX.ElementAttributesProperty {}
export interface ElementChildrenAttribute
extends ThemeUIJSX.ElementChildrenAttribute {}
export type LibraryManagedAttributes<C, P> =
ThemeUIJSX.LibraryManagedAttributes<C, P>
export interface IntrinsicAttributes
extends ThemeUIJSX.IntrinsicAttributes {}
export interface IntrinsicClassAttributes<T>
extends ThemeUIJSX.IntrinsicClassAttributes<T> {}
export type IntrinsicElements = ThemeUIJSX.IntrinsicElements
}
}
export interface ThemeUIContextValue {
__EMOTION_VERSION__: string
theme: Theme
}
/**
* @internal
*/
export const __themeUiDefaultContextValue: ThemeUIContextValue = {
__EMOTION_VERSION__,
theme: {},
}
/**
* @internal
*/
export const __ThemeUIContext = React.createContext(
__themeUiDefaultContextValue
)
export const useThemeUI = () => React.useContext(__ThemeUIContext)
const canUseSymbol = typeof Symbol === 'function' && Symbol.for
const REACT_ELEMENT = canUseSymbol ? Symbol.for('react.element') : 0xeac7
const FORWARD_REF = canUseSymbol ? Symbol.for('react.forward_ref') : 0xeac7
const deepmergeOptions: deepmerge.Options = {
isMergeableObject: (n) => {
return (
!!n &&
typeof n === 'object' &&
(n as React.ExoticComponent).$$typeof !== REACT_ELEMENT &&
(n as React.ExoticComponent).$$typeof !== FORWARD_REF
)
},
arrayMerge: (_leftArray, rightArray) => rightArray,
}
/**
* Deeply merge themes
*/
export const merge = (a: Theme, b: Theme): Theme =>
deepmerge(a, b, deepmergeOptions)
function mergeAll<A, B>(a: A, B: B): A & B
function mergeAll<A, B, C>(a: A, B: B, c: C): A & B & C
function mergeAll<A, B, C, D>(a: A, B: B, c: C, d: D): A & B & C & D
function mergeAll<T = Theme>(...args: Partial<T>[]) {
return deepmerge.all<T>(args, deepmergeOptions)
}
merge.all = mergeAll
export interface __ThemeUIInternalBaseThemeProviderProps {
context: ThemeUIContextValue
children: React.ReactNode
}
/**
* @internal
*/
export const __ThemeUIInternalBaseThemeProvider = ({
context,
children,
}: __ThemeUIInternalBaseThemeProviderProps) =>
jsx(
EmotionContext.Provider,
{ value: context.theme },
jsx(__ThemeUIContext.Provider, {
value: context,
children,
})
)
export interface ThemeProviderProps {
theme: Theme | ((outerTheme: Theme) => Theme)
children?: React.ReactNode
}
export function ThemeProvider({ theme, children }: ThemeProviderProps) {
const outer = useThemeUI()
if (process.env.NODE_ENV !== 'production') {
if (outer.__EMOTION_VERSION__ !== __EMOTION_VERSION__) {
console.warn(
'Multiple versions of Emotion detected,',
'and theming might not work as expected.',
'Please ensure there is only one copy of @emotion/react installed in your application.'
)
}
}
const context =
typeof theme === 'function'
? { ...outer, theme: theme(outer.theme) }
: merge.all({}, outer, { theme })
return jsx(__ThemeUIInternalBaseThemeProvider, { context, children })
}