Spaces:
Sleeping
Sleeping
File size: 6,954 Bytes
56fda74 |
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
import isBrowser from '#is-browser'
import isDevelopment from '#is-development'
import { Theme, ThemeContext, withEmotionCache } from '@emotion/react'
import { Interpolation, serializeStyles } from '@emotion/serialize'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
import {
EmotionCache,
getRegisteredStyles,
insertStyles,
registerStyles,
SerializedStyles
} from '@emotion/utils'
import * as React from 'react'
import { CreateStyled, ElementType, StyledOptions } from './types'
import { composeShouldForwardProps, getDefaultShouldForwardProp } from './utils'
export type {
ArrayInterpolation,
ComponentSelector,
CSSObject,
FunctionInterpolation,
Interpolation
} from '@emotion/serialize'
const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
Because you write your CSS inside a JavaScript string you actually have to do double escaping, so for example "content: '\\00d7';" should become "content: '\\\\00d7';".
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#ES2018_revision_of_illegal_escape_sequences`
const Insertion = ({
cache,
serialized,
isStringTag
}: {
cache: EmotionCache
serialized: SerializedStyles
isStringTag: boolean
}) => {
registerStyles(cache, serialized, isStringTag)
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
insertStyles(cache, serialized, isStringTag)
)
if (!isBrowser && rules !== undefined) {
let serializedNames = serialized.name
let next = serialized.next
while (next !== undefined) {
serializedNames += ' ' + next.name
next = next.next
}
return (
<style
{...{
[`data-emotion`]: `${cache.key} ${serializedNames}`,
dangerouslySetInnerHTML: { __html: rules },
nonce: cache.sheet.nonce
}}
/>
)
}
return null
}
const createStyled = (tag: ElementType, options?: StyledOptions) => {
if (isDevelopment) {
if (tag === undefined) {
throw new Error(
'You are trying to create a styled element with an undefined component.\nYou may have forgotten to import it.'
)
}
}
const isReal = tag.__emotion_real === tag
const baseTag = (isReal && tag.__emotion_base) || tag
let identifierName: string | undefined
let targetClassName: string | undefined
if (options !== undefined) {
identifierName = options.label
targetClassName = options.target
}
const shouldForwardProp = composeShouldForwardProps(tag, options, isReal)
const defaultShouldForwardProp =
shouldForwardProp || getDefaultShouldForwardProp(baseTag)
const shouldUseAs = !defaultShouldForwardProp('as')
return function () {
// eslint-disable-next-line prefer-rest-params
let args = arguments as any as Array<
TemplateStringsArray | Interpolation<Theme>
>
let styles =
isReal && tag.__emotion_styles !== undefined
? tag.__emotion_styles.slice(0)
: []
if (identifierName !== undefined) {
styles.push(`label:${identifierName};`)
}
if (
args[0] == null ||
(args[0] as TemplateStringsArray).raw === undefined
) {
// eslint-disable-next-line prefer-spread
styles.push.apply(styles, args)
} else {
const templateStringsArr = args[0] as TemplateStringsArray
if (isDevelopment && templateStringsArr[0] === undefined) {
console.error(ILLEGAL_ESCAPE_SEQUENCE_ERROR)
}
styles.push(templateStringsArr[0])
let len = args.length
let i = 1
for (; i < len; i++) {
if (isDevelopment && templateStringsArr[i] === undefined) {
console.error(ILLEGAL_ESCAPE_SEQUENCE_ERROR)
}
styles.push(args[i], templateStringsArr[i])
}
}
const Styled: ElementType = withEmotionCache(
(props: Record<string, unknown>, cache, ref) => {
const FinalTag =
(shouldUseAs && (props.as as React.ElementType)) || baseTag
let className = ''
let classInterpolations: Interpolation<Theme>[] = []
let mergedProps = props
if (props.theme == null) {
mergedProps = {}
for (let key in props) {
mergedProps[key] = props[key]
}
mergedProps.theme = React.useContext(ThemeContext)
}
if (typeof props.className === 'string') {
className = getRegisteredStyles(
cache.registered,
classInterpolations,
props.className
)
} else if (props.className != null) {
className = `${props.className} `
}
const serialized = serializeStyles(
styles.concat(classInterpolations),
cache.registered,
mergedProps
)
className += `${cache.key}-${serialized.name}`
if (targetClassName !== undefined) {
className += ` ${targetClassName}`
}
const finalShouldForwardProp =
shouldUseAs && shouldForwardProp === undefined
? getDefaultShouldForwardProp(FinalTag)
: defaultShouldForwardProp
let newProps: Record<string, unknown> = {}
for (let key in props) {
if (shouldUseAs && key === 'as') continue
if (finalShouldForwardProp(key)) {
newProps[key] = props[key]
}
}
newProps.className = className
if (ref) {
newProps.ref = ref
}
return (
<>
<Insertion
cache={cache}
serialized={serialized}
isStringTag={typeof FinalTag === 'string'}
/>
<FinalTag {...newProps} />
</>
)
}
)
Styled.displayName =
identifierName !== undefined
? identifierName
: `Styled(${
typeof baseTag === 'string'
? baseTag
: baseTag.displayName || baseTag.name || 'Component'
})`
Styled.defaultProps = tag.defaultProps
Styled.__emotion_real = Styled
Styled.__emotion_base = baseTag
Styled.__emotion_styles = styles
Styled.__emotion_forwardProp = shouldForwardProp
Object.defineProperty(Styled, 'toString', {
value() {
if (targetClassName === undefined && isDevelopment) {
return 'NO_COMPONENT_SELECTOR'
}
return `.${targetClassName}`
}
})
;(Styled as any).withComponent = (
nextTag: ElementType,
nextOptions: StyledOptions
) => {
const newStyled = createStyled(nextTag, {
...options,
...nextOptions,
shouldForwardProp: composeShouldForwardProps(Styled, nextOptions, true)
})
return (newStyled as any)(...styles)
}
return Styled
}
}
export default createStyled as CreateStyled
|