@octanejs/styled-components 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +103 -0
- package/package.json +60 -0
- package/src/constants.ts +46 -0
- package/src/constructors/constructWithOptions.ts +171 -0
- package/src/constructors/createGlobalStyle.ts +165 -0
- package/src/constructors/createTheme.ts +233 -0
- package/src/constructors/css.ts +64 -0
- package/src/constructors/keyframes.ts +36 -0
- package/src/constructors/styled.ts +52 -0
- package/src/hoc/withTheme.ts +40 -0
- package/src/index.ts +111 -0
- package/src/models/ComponentStyle.ts +171 -0
- package/src/models/GlobalStyle.ts +129 -0
- package/src/models/Keyframes.ts +66 -0
- package/src/models/ServerStyleSheet.ts +92 -0
- package/src/models/StyleSheetManager.ts +210 -0
- package/src/models/StyledComponent.ts +346 -0
- package/src/models/ThemeProvider.ts +124 -0
- package/src/secretInternals.ts +7 -0
- package/src/sheet/GroupIDAllocator.ts +45 -0
- package/src/sheet/GroupedTag.ts +125 -0
- package/src/sheet/Rehydration.ts +188 -0
- package/src/sheet/RuleOutput.ts +66 -0
- package/src/sheet/Sheet.ts +162 -0
- package/src/sheet/Tag.ts +135 -0
- package/src/sheet/dom.ts +64 -0
- package/src/sheet/index.ts +2 -0
- package/src/sheet/octaneChannel.ts +57 -0
- package/src/sheet/types.ts +56 -0
- package/src/types.ts +294 -0
- package/src/utils/addUnitIfNeeded.ts +59 -0
- package/src/utils/charCodes.ts +18 -0
- package/src/utils/checkDynamicCreation.ts +28 -0
- package/src/utils/createWarnTooManyClasses.ts +33 -0
- package/src/utils/cssTagged.ts +3 -0
- package/src/utils/determineTheme.ts +10 -0
- package/src/utils/domElements.ts +7 -0
- package/src/utils/empties.ts +4 -0
- package/src/utils/error.ts +41 -0
- package/src/utils/errors.ts +20 -0
- package/src/utils/escape.ts +11 -0
- package/src/utils/flatten.ts +175 -0
- package/src/utils/generateAlphabeticName.ts +21 -0
- package/src/utils/generateComponentId.ts +6 -0
- package/src/utils/generateDisplayName.ts +7 -0
- package/src/utils/getComponentName.ts +10 -0
- package/src/utils/hash.ts +26 -0
- package/src/utils/hoist.ts +83 -0
- package/src/utils/hyphenateStyleName.ts +33 -0
- package/src/utils/interleave.ts +14 -0
- package/src/utils/isFunction.ts +3 -0
- package/src/utils/isKeyframes.ts +9 -0
- package/src/utils/isPlainObject.ts +11 -0
- package/src/utils/isStatelessFunction.ts +5 -0
- package/src/utils/isStaticRules.ts +17 -0
- package/src/utils/isStyledComponent.ts +19 -0
- package/src/utils/isTag.ts +10 -0
- package/src/utils/joinStrings.ts +26 -0
- package/src/utils/mixinDeep.ts +34 -0
- package/src/utils/nonce.ts +33 -0
- package/src/utils/setToString.ts +19 -0
- package/src/utils/stylis.ts +378 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type StyleSheet from '../sheet';
|
|
2
|
+
import {
|
|
3
|
+
AnyComponent,
|
|
4
|
+
Dict,
|
|
5
|
+
ExecutionContext,
|
|
6
|
+
Interpolation,
|
|
7
|
+
IStyledComponent,
|
|
8
|
+
RuleSet,
|
|
9
|
+
Stringifier,
|
|
10
|
+
} from '../types';
|
|
11
|
+
import { cssTagged } from './cssTagged';
|
|
12
|
+
import addUnitIfNeeded from './addUnitIfNeeded';
|
|
13
|
+
import getComponentName from './getComponentName';
|
|
14
|
+
import hyphenate from './hyphenateStyleName';
|
|
15
|
+
import isFunction from './isFunction';
|
|
16
|
+
import isKeyframes from './isKeyframes';
|
|
17
|
+
import isPlainObject from './isPlainObject';
|
|
18
|
+
import isStatelessFunction from './isStatelessFunction';
|
|
19
|
+
import isStyledComponent from './isStyledComponent';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* It's falsish not falsy because 0 is allowed.
|
|
23
|
+
*/
|
|
24
|
+
const isFalsish = (chunk: any): chunk is undefined | null | false | '' =>
|
|
25
|
+
chunk === undefined || chunk === null || chunk === false || chunk === '';
|
|
26
|
+
|
|
27
|
+
const CLIENT_REFERENCE = Symbol.for('react.client.reference');
|
|
28
|
+
|
|
29
|
+
interface ClientReference {
|
|
30
|
+
$$typeof: symbol;
|
|
31
|
+
$$id?: string;
|
|
32
|
+
$$async?: boolean;
|
|
33
|
+
name?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isClientReference(chunk: unknown): chunk is ClientReference {
|
|
37
|
+
return (chunk as any).$$typeof === CLIENT_REFERENCE;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// React encodes $$id as "modulePath#exportName"
|
|
41
|
+
function warnClientReference(ref: ClientReference): void {
|
|
42
|
+
const id = ref.$$id;
|
|
43
|
+
const label = (id && id.includes('#') ? id.split('#').pop() : id) || ref.name || 'unknown';
|
|
44
|
+
console.warn(
|
|
45
|
+
`Interpolating a client component (${label}) as a selector is not supported in server components. The component selector pattern requires access to the component's internal class name, which is not available across the server/client boundary. Use a plain CSS class selector instead.`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Internal accumulator form - avoids allocating a temp array per nesting level. */
|
|
50
|
+
function objToCssArrayInto(obj: Dict<any>, rules: any[]): void {
|
|
51
|
+
for (const key in obj) {
|
|
52
|
+
const val = obj[key];
|
|
53
|
+
if (!obj.hasOwnProperty(key) || isFalsish(val)) continue;
|
|
54
|
+
|
|
55
|
+
if ((Array.isArray(val) && cssTagged.has(val)) || isFunction(val)) {
|
|
56
|
+
rules.push(hyphenate(key) + ':', val, ';');
|
|
57
|
+
} else if (isPlainObject(val)) {
|
|
58
|
+
rules.push(key + ' {');
|
|
59
|
+
objToCssArrayInto(val, rules);
|
|
60
|
+
rules.push('}');
|
|
61
|
+
} else {
|
|
62
|
+
rules.push(hyphenate(key) + ': ' + addUnitIfNeeded(key, val) + ';');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const objToCssArray = (obj: Dict<any>): string[] => {
|
|
68
|
+
const rules: any[] = [];
|
|
69
|
+
objToCssArrayInto(obj, rules);
|
|
70
|
+
return rules;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export default function flatten<Props extends object>(
|
|
74
|
+
chunk: Interpolation<object>,
|
|
75
|
+
executionContext?: (ExecutionContext & Props) | undefined,
|
|
76
|
+
styleSheet?: StyleSheet | undefined,
|
|
77
|
+
stylisInstance?: Stringifier | undefined,
|
|
78
|
+
result: RuleSet<Props> = [],
|
|
79
|
+
): RuleSet<Props> {
|
|
80
|
+
if (isFalsish(chunk)) {
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const t = typeof chunk;
|
|
85
|
+
|
|
86
|
+
if (t === 'string') {
|
|
87
|
+
result.push(chunk as string);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (t === 'function') {
|
|
92
|
+
if (isClientReference(chunk)) {
|
|
93
|
+
if (process.env.NODE_ENV !== 'production') warnClientReference(chunk);
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// OCTANE: styled components are plain functions (no forwardRef exotic
|
|
98
|
+
// object), so the component-selector case must be handled BEFORE the
|
|
99
|
+
// interpolation-function branch or `${Button}` would invoke Button's
|
|
100
|
+
// render body as if it were a style function. Upstream reaches its
|
|
101
|
+
// object-typed isStyledComponent check below instead.
|
|
102
|
+
if (isStyledComponent(chunk)) {
|
|
103
|
+
result.push(`.${(chunk as unknown as IStyledComponent<'web', any>).styledComponentId}`);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (isStatelessFunction(chunk) && executionContext) {
|
|
108
|
+
const fnResult = (chunk as Function)(executionContext);
|
|
109
|
+
|
|
110
|
+
if (
|
|
111
|
+
process.env.NODE_ENV !== 'production' &&
|
|
112
|
+
typeof fnResult === 'object' &&
|
|
113
|
+
!Array.isArray(fnResult) &&
|
|
114
|
+
!isKeyframes(fnResult) &&
|
|
115
|
+
!isPlainObject(fnResult) &&
|
|
116
|
+
fnResult !== null
|
|
117
|
+
) {
|
|
118
|
+
console.error(
|
|
119
|
+
`${getComponentName(
|
|
120
|
+
chunk as AnyComponent,
|
|
121
|
+
)} is not a styled component and cannot be referred to via component selector. See https://styled-components.com/docs/advanced#referring-to-other-components for more details.`,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return flatten<Props>(fnResult, executionContext, styleSheet, stylisInstance, result);
|
|
126
|
+
} else {
|
|
127
|
+
result.push(chunk as unknown as IStyledComponent<'web'>);
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (Array.isArray(chunk)) {
|
|
133
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
134
|
+
flatten<Props>(chunk[i], executionContext, styleSheet, stylisInstance, result);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (isStyledComponent(chunk)) {
|
|
140
|
+
result.push(`.${(chunk as unknown as IStyledComponent<'web', any>).styledComponentId}`);
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (isKeyframes(chunk)) {
|
|
145
|
+
if (styleSheet) {
|
|
146
|
+
chunk.inject(styleSheet, stylisInstance);
|
|
147
|
+
result.push(chunk.getName(stylisInstance));
|
|
148
|
+
} else {
|
|
149
|
+
result.push(chunk);
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Module-level client reference proxies (typeof 'object') pass isPlainObject - catch before
|
|
155
|
+
if (isClientReference(chunk)) {
|
|
156
|
+
if (process.env.NODE_ENV !== 'production') warnClientReference(chunk);
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (isPlainObject(chunk)) {
|
|
161
|
+
// A plain object that defines its own toString takes the stringification
|
|
162
|
+
// path - it's being interpolated as a value (e.g. a design token), not as
|
|
163
|
+
// a CSS-in-JS rule object. Without this, `${token}` in a tagged template
|
|
164
|
+
// would walk the object's enumerable keys as CSS declarations. (#5740)
|
|
165
|
+
if ((chunk as object).toString !== Object.prototype.toString) {
|
|
166
|
+
result.push((chunk as object).toString());
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
objToCssArrayInto(chunk as Dict<any>, result);
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
result.push((chunk as any).toString());
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const AD_REPLACER_R = /(a)(d)/gi;
|
|
2
|
+
|
|
3
|
+
/* This is the "capacity" of our alphabet i.e. 2x26 for all letters plus their capitalised
|
|
4
|
+
* counterparts */
|
|
5
|
+
const charsLength = 52;
|
|
6
|
+
|
|
7
|
+
/* start at 75 for 'a' until 'z' (25) and then start at 65 for capitalised letters */
|
|
8
|
+
const getAlphabeticChar = (code: number) => String.fromCharCode(code + (code > 25 ? 39 : 97));
|
|
9
|
+
|
|
10
|
+
/* input a number, usually a hash and convert it to base-52 */
|
|
11
|
+
export default function generateAlphabeticName(code: number) {
|
|
12
|
+
let name = '';
|
|
13
|
+
let x;
|
|
14
|
+
|
|
15
|
+
/* get a char and divide by alphabet-length */
|
|
16
|
+
for (x = Math.abs(code); x > charsLength; x = (x / charsLength) | 0) {
|
|
17
|
+
name = getAlphabeticChar(x % charsLength) + name;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return (getAlphabeticChar(x % charsLength) + name).replace(AD_REPLACER_R, '$1-$2');
|
|
21
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { StyledTarget } from '../types';
|
|
2
|
+
import getComponentName from './getComponentName';
|
|
3
|
+
import isTag from './isTag';
|
|
4
|
+
|
|
5
|
+
export default function generateDisplayName(target: StyledTarget<any>) {
|
|
6
|
+
return isTag(target) ? `styled.${target}` : `Styled(${getComponentName(target)})`;
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StyledTarget } from '../types';
|
|
2
|
+
|
|
3
|
+
export default function getComponentName(target: StyledTarget<any>) {
|
|
4
|
+
return (
|
|
5
|
+
(process.env.NODE_ENV !== 'production' ? typeof target === 'string' && target : false) ||
|
|
6
|
+
(target as Exclude<StyledTarget<any>, string>).displayName ||
|
|
7
|
+
(target as Function).name ||
|
|
8
|
+
'Component'
|
|
9
|
+
);
|
|
10
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const SEED = 5381;
|
|
2
|
+
|
|
3
|
+
// When we have separate strings it's useful to run a progressive
|
|
4
|
+
// version of djb2 where we pretend that we're still looping over
|
|
5
|
+
// the same string
|
|
6
|
+
export const phash = (h: number, x: string) => {
|
|
7
|
+
let i = x.length;
|
|
8
|
+
|
|
9
|
+
while (i) {
|
|
10
|
+
h = (h * 33) ^ x.charCodeAt(--i);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return h;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Hash a non-negative integer, avoiding String() allocation for single digits.
|
|
17
|
+
// Equivalent to phash(h, String(n)) for n >= 0.
|
|
18
|
+
export const phashN = (h: number, n: number) => {
|
|
19
|
+
if (n >= 0 && n < 10) return (h * 33) ^ (n + 48);
|
|
20
|
+
return phash(h, '' + n);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// This is a djb2 hashing function
|
|
24
|
+
export const hash = (x: string) => {
|
|
25
|
+
return phash(SEED, x);
|
|
26
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Ported from styled-components 6.4.3 (MIT). Octane adaptation of the inlined
|
|
2
|
+
// hoist-non-react-statics: octane components are plain functions (no
|
|
3
|
+
// forwardRef/memo exotic objects), so the React `$$typeof` static tables
|
|
4
|
+
// collapse to one component-statics list.
|
|
5
|
+
import { AnyComponent } from '../types';
|
|
6
|
+
import { STYLED_COMPONENT_BRAND } from './isStyledComponent';
|
|
7
|
+
|
|
8
|
+
const COMPONENT_STATICS = {
|
|
9
|
+
defaultProps: true,
|
|
10
|
+
displayName: true,
|
|
11
|
+
propTypes: true,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const KNOWN_STATICS = {
|
|
15
|
+
name: true,
|
|
16
|
+
length: true,
|
|
17
|
+
prototype: true,
|
|
18
|
+
caller: true,
|
|
19
|
+
callee: true,
|
|
20
|
+
arguments: true,
|
|
21
|
+
arity: true,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const defineProperty = Object.defineProperty;
|
|
25
|
+
const getOwnPropertyNames = Object.getOwnPropertyNames;
|
|
26
|
+
const getOwnPropertySymbols = Object.getOwnPropertySymbols;
|
|
27
|
+
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
|
|
28
|
+
const getPrototypeOf = Object.getPrototypeOf;
|
|
29
|
+
const objectPrototype = Object.prototype;
|
|
30
|
+
|
|
31
|
+
type ExcludeList = {
|
|
32
|
+
[key: string]: true;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type NonOctaneStatics<S extends AnyComponent, C extends ExcludeList = {}> = {
|
|
36
|
+
[key in Exclude<
|
|
37
|
+
keyof S,
|
|
38
|
+
keyof typeof COMPONENT_STATICS | keyof typeof KNOWN_STATICS | keyof C
|
|
39
|
+
>]: S[key];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default function hoistNonOctaneStatics<
|
|
43
|
+
T extends AnyComponent,
|
|
44
|
+
S extends AnyComponent,
|
|
45
|
+
C extends ExcludeList = {},
|
|
46
|
+
>(targetComponent: T, sourceComponent: S, excludelist?: C | undefined) {
|
|
47
|
+
if (typeof sourceComponent !== 'string') {
|
|
48
|
+
// don't hoist over string (html) components
|
|
49
|
+
|
|
50
|
+
const inheritedComponent = getPrototypeOf(sourceComponent);
|
|
51
|
+
if (inheritedComponent && inheritedComponent !== objectPrototype) {
|
|
52
|
+
hoistNonOctaneStatics(targetComponent, inheritedComponent, excludelist);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const keys: (string | symbol)[] = (
|
|
56
|
+
getOwnPropertyNames(sourceComponent) as (string | symbol)[]
|
|
57
|
+
).concat(getOwnPropertySymbols(sourceComponent));
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < keys.length; ++i) {
|
|
60
|
+
const key = keys[i] as string;
|
|
61
|
+
if (
|
|
62
|
+
// The styled brand must never transfer to a wrapping component: a HOC
|
|
63
|
+
// that hoists a styled component's statics is not itself styleable via
|
|
64
|
+
// component selectors (see utils/isStyledComponent.ts).
|
|
65
|
+
(key as unknown) !== STYLED_COMPONENT_BRAND &&
|
|
66
|
+
!(key in KNOWN_STATICS) &&
|
|
67
|
+
!(key in COMPONENT_STATICS) &&
|
|
68
|
+
!(excludelist && excludelist[key])
|
|
69
|
+
) {
|
|
70
|
+
const descriptor = getOwnPropertyDescriptor(sourceComponent, key);
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
// Avoid failures from read-only properties
|
|
74
|
+
defineProperty(targetComponent, key, descriptor!);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
/* ignore */
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return targetComponent as T & NonOctaneStatics<S, C>;
|
|
83
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { HYPHEN, UPPER_A, UPPER_Z, UPPER_TO_LOWER } from './charCodes';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hyphenates a camelcased CSS property name, for example:
|
|
5
|
+
*
|
|
6
|
+
* > hyphenateStyleName('backgroundColor')
|
|
7
|
+
* < "background-color"
|
|
8
|
+
* > hyphenateStyleName('MozTransition')
|
|
9
|
+
* < "-moz-transition"
|
|
10
|
+
* > hyphenateStyleName('msTransition')
|
|
11
|
+
* < "-ms-transition"
|
|
12
|
+
*
|
|
13
|
+
* As Modernizr suggests (http://modernizr.com/docs/#prefixed), an `ms` prefix
|
|
14
|
+
* is converted to `-ms-`.
|
|
15
|
+
*/
|
|
16
|
+
export default function hyphenateStyleName(string: string): string {
|
|
17
|
+
// CSS variable prefix (`--*`) passes through untouched.
|
|
18
|
+
if (string.charCodeAt(0) === HYPHEN && string.charCodeAt(1) === HYPHEN) {
|
|
19
|
+
return string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let output = '';
|
|
23
|
+
for (let i = 0; i < string.length; i++) {
|
|
24
|
+
const code = string.charCodeAt(i);
|
|
25
|
+
if (code >= UPPER_A && code <= UPPER_Z) {
|
|
26
|
+
output += '-' + String.fromCharCode(code + UPPER_TO_LOWER);
|
|
27
|
+
} else {
|
|
28
|
+
output += string[i];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return output.startsWith('ms-') ? '-' + output : output;
|
|
33
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Interpolation } from '../types';
|
|
2
|
+
|
|
3
|
+
export default function interleave<Props extends object>(
|
|
4
|
+
strings: readonly string[],
|
|
5
|
+
interpolations: Interpolation<Props>[],
|
|
6
|
+
): Interpolation<Props>[] {
|
|
7
|
+
const result: Interpolation<Props>[] = [strings[0]];
|
|
8
|
+
|
|
9
|
+
for (let i = 0, len = interpolations.length; i < len; i += 1) {
|
|
10
|
+
result.push(interpolations[i], strings[i + 1]);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type KeyframesClass from '../models/Keyframes';
|
|
2
|
+
|
|
3
|
+
const KEYFRAMES_SYMBOL = Symbol.for('sc-keyframes');
|
|
4
|
+
|
|
5
|
+
export default function isKeyframes(value: unknown): value is KeyframesClass {
|
|
6
|
+
return typeof value === 'object' && value !== null && KEYFRAMES_SYMBOL in value;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { KEYFRAMES_SYMBOL };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function isPlainObject(x: any): x is Record<any, any> {
|
|
2
|
+
return (
|
|
3
|
+
x !== null &&
|
|
4
|
+
typeof x === 'object' &&
|
|
5
|
+
x.constructor.name === Object.name &&
|
|
6
|
+
/* check for reasonable markers that the object isn't an element for react & preact/compat */
|
|
7
|
+
!('props' in x && x.$$typeof) &&
|
|
8
|
+
/* octane element descriptors carry $$kind instead of $$typeof */
|
|
9
|
+
!('props' in x && x.$$kind)
|
|
10
|
+
);
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RuleSet } from '../types';
|
|
2
|
+
import isFunction from './isFunction';
|
|
3
|
+
import isStyledComponent from './isStyledComponent';
|
|
4
|
+
|
|
5
|
+
export default function isStaticRules<Props extends object>(rules: RuleSet<Props>) {
|
|
6
|
+
for (let i = 0; i < rules.length; i += 1) {
|
|
7
|
+
const rule = rules[i];
|
|
8
|
+
|
|
9
|
+
if (isFunction(rule) && !isStyledComponent(rule)) {
|
|
10
|
+
// functions are allowed to be static if they're just being
|
|
11
|
+
// used to get the classname of a nested styled component
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { StyledComponentBrand } from '../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Octane adaptation: upstream brands styled components with React's
|
|
5
|
+
* forward-ref `$$typeof` so HOCs that hoist statics (and therefore inherit
|
|
6
|
+
* `styledComponentId`) are not mistaken for real styled components. Octane
|
|
7
|
+
* has no forwardRef, so the factory stamps this dedicated symbol instead —
|
|
8
|
+
* `hoist` explicitly never copies it.
|
|
9
|
+
*/
|
|
10
|
+
export const STYLED_COMPONENT_BRAND = Symbol.for('@octanejs/styled-components:brand');
|
|
11
|
+
|
|
12
|
+
export default function isStyledComponent(target: any): target is StyledComponentBrand {
|
|
13
|
+
return (
|
|
14
|
+
target != null &&
|
|
15
|
+
(typeof target === 'object' || typeof target === 'function') &&
|
|
16
|
+
(target as any)[STYLED_COMPONENT_BRAND] === true &&
|
|
17
|
+
'styledComponentId' in target
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StyledTarget } from '../types';
|
|
2
|
+
|
|
3
|
+
export default function isTag(target: StyledTarget<'web'>): target is string {
|
|
4
|
+
return (
|
|
5
|
+
typeof target === 'string' &&
|
|
6
|
+
(process.env.NODE_ENV !== 'production'
|
|
7
|
+
? target.charAt(0) === target.charAt(0).toLowerCase()
|
|
8
|
+
: true)
|
|
9
|
+
);
|
|
10
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SPLITTER } from '../constants';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convenience function for joining strings to form className chains
|
|
5
|
+
*/
|
|
6
|
+
export function joinStrings(a?: string | undefined, b?: string | undefined): string {
|
|
7
|
+
return a && b ? a + ' ' + b : a || b || '';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function joinStringArray(arr: string[], sep?: string | undefined): string {
|
|
11
|
+
return arr.join(sep || '');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Join compiled CSS rules with the SC splitter delimiter. */
|
|
15
|
+
export function joinRules(rules: string[]): string {
|
|
16
|
+
let css = '';
|
|
17
|
+
for (let i = 0; i < rules.length; i++) {
|
|
18
|
+
css += rules[i] + SPLITTER;
|
|
19
|
+
}
|
|
20
|
+
return css;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function stripSplitter(css: string): string {
|
|
24
|
+
if (!css) return css;
|
|
25
|
+
return css.replaceAll(SPLITTER, '');
|
|
26
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import isPlainObject from './isPlainObject';
|
|
2
|
+
|
|
3
|
+
function mixinRecursively(target: any, source: any, forceMerge = false) {
|
|
4
|
+
/* only merge into POJOs, Arrays, but for top level objects only
|
|
5
|
+
* allow to merge into anything by passing forceMerge = true */
|
|
6
|
+
if (!forceMerge && !isPlainObject(target) && !Array.isArray(target)) {
|
|
7
|
+
return source;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (Array.isArray(source)) {
|
|
11
|
+
for (let key = 0; key < source.length; key++) {
|
|
12
|
+
target[key] = mixinRecursively(target[key], source[key]);
|
|
13
|
+
}
|
|
14
|
+
} else if (isPlainObject(source)) {
|
|
15
|
+
for (const key in source) {
|
|
16
|
+
target[key] = mixinRecursively(target[key], source[key]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return target;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Arrays & POJOs merged recursively, other objects and value types are overridden
|
|
25
|
+
* If target is not a POJO or an Array, it will get source properties injected via shallow merge
|
|
26
|
+
* Source objects applied left to right. Mutates & returns target. Similar to lodash merge.
|
|
27
|
+
*/
|
|
28
|
+
export default function mixinDeep(target: any, ...sources: any[]) {
|
|
29
|
+
for (const source of sources) {
|
|
30
|
+
mixinRecursively(target, source, true);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return target;
|
|
34
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
declare let __webpack_nonce__: string;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolve a CSP nonce from available sources (in priority order):
|
|
5
|
+
* 1. <meta property="csp-nonce"> (Vite puts nonce in the `nonce` attr)
|
|
6
|
+
* 2. <meta name="sc-nonce"> (SC convention, nonce in `content` attr)
|
|
7
|
+
* 3. __webpack_nonce__ global (legacy)
|
|
8
|
+
*
|
|
9
|
+
* For Next.js/Remix, pass nonces explicitly via StyleSheetManager or
|
|
10
|
+
* ServerStyleSheet instead - auto-detection doesn't apply to header-based nonces.
|
|
11
|
+
*/
|
|
12
|
+
let cached: string | undefined | false = false;
|
|
13
|
+
|
|
14
|
+
/** @internal Reset the nonce cache (for testing only). */
|
|
15
|
+
export function resetNonceCache() {
|
|
16
|
+
cached = false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default function getNonce(): string | undefined {
|
|
20
|
+
if (cached !== false) return cached;
|
|
21
|
+
|
|
22
|
+
if (typeof document !== 'undefined') {
|
|
23
|
+
// Vite sets the nonce in the `nonce` attribute. Browsers expose this via
|
|
24
|
+
// the .nonce DOM property but return "" from getAttribute('nonce').
|
|
25
|
+
const viteMeta = document.head.querySelector<HTMLMetaElement>('meta[property="csp-nonce"]');
|
|
26
|
+
if (viteMeta) return (cached = viteMeta.nonce || viteMeta.getAttribute('content') || undefined);
|
|
27
|
+
|
|
28
|
+
const scMeta = document.head.querySelector<HTMLMetaElement>('meta[name="sc-nonce"]');
|
|
29
|
+
if (scMeta) return (cached = scMeta.getAttribute('content') || undefined);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (cached = typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : undefined);
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* If the Object prototype is frozen, the "toString" property is non-writable. This means that any objects which inherit this property
|
|
3
|
+
* cannot have the property changed using a "=" assignment operator. If using strict mode, attempting that will cause an error. If not using
|
|
4
|
+
* strict mode, attempting that will be silently ignored.
|
|
5
|
+
*
|
|
6
|
+
* If the Object prototype is frozen, inherited non-writable properties can still be shadowed using one of two mechanisms:
|
|
7
|
+
*
|
|
8
|
+
* 1. ES6 class methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#methods
|
|
9
|
+
* 2. Using the `Object.defineProperty()` static method:
|
|
10
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
|
|
11
|
+
*
|
|
12
|
+
* However, this project uses Babel to transpile ES6 classes, and transforms ES6 class methods to use the assignment operator instead:
|
|
13
|
+
* https://babeljs.io/docs/babel-plugin-transform-class-properties#options
|
|
14
|
+
*
|
|
15
|
+
* Therefore, the most compatible way to shadow the prototype's "toString" property is to define a new "toString" property on this object.
|
|
16
|
+
*/
|
|
17
|
+
export function setToString(object: object, toStringFn: () => string) {
|
|
18
|
+
Object.defineProperty(object, 'toString', { value: toStringFn });
|
|
19
|
+
}
|