@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dominic Gannaway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @octanejs/styled-components
|
|
2
|
+
|
|
3
|
+
[styled-components](https://styled-components.com) for the
|
|
4
|
+
[octane](https://github.com/octanejs/octane) renderer — an API- and
|
|
5
|
+
behavior-compatible port of the styled-components v6 web surface, built from
|
|
6
|
+
the upstream 6.4.3 sources.
|
|
7
|
+
|
|
8
|
+
Everything you'd import from `styled-components` works here: `styled` with
|
|
9
|
+
every HTML/SVG tag shortcut, `.attrs`/`.withConfig` chaining, `css`,
|
|
10
|
+
`keyframes`, `createGlobalStyle`, `createTheme`, theming (`ThemeProvider`,
|
|
11
|
+
`ThemeContext`, `ThemeConsumer`, `useTheme`, `withTheme`), `StyleSheetManager`
|
|
12
|
+
(custom targets, namespaces, vendor prefixing, stylis plugins,
|
|
13
|
+
`shouldForwardProp`), `ServerStyleSheet`, and `isStyledComponent` — including
|
|
14
|
+
component selectors, `styled(Styled)` folding, transient `$` props, and
|
|
15
|
+
`as`/`forwardedAs` polymorphism. The React Native surface and the RSC-only
|
|
16
|
+
`stylisPluginRSC` are not ported.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @octanejs/styled-components
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`octane` is a peer dependency.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import styled, { ThemeProvider, createGlobalStyle } from '@octanejs/styled-components';
|
|
30
|
+
|
|
31
|
+
const Global = createGlobalStyle`
|
|
32
|
+
body { margin: 0; font-family: system-ui; }
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
const Button = styled.button<{ $primary?: boolean }>`
|
|
36
|
+
padding: 0.5em 1em;
|
|
37
|
+
color: ${props => (props.$primary ? 'white' : props.theme.fg)};
|
|
38
|
+
background: ${props => (props.$primary ? props.theme.accent : 'transparent')};
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
export function App() @{
|
|
42
|
+
<ThemeProvider theme={{ fg: 'black', accent: 'rebeccapurple' }}>
|
|
43
|
+
<Global />
|
|
44
|
+
<Button $primary onClick={() => console.log('hi')}>Save</Button>
|
|
45
|
+
</ThemeProvider>
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Server rendering
|
|
50
|
+
|
|
51
|
+
SSR needs **no setup**. Styled rules, keyframes, and global styles rendered on
|
|
52
|
+
the server flow through octane's css channel, so `renderToString` returns them
|
|
53
|
+
in `RenderResult.css` (and `renderToPipeableStream`/`renderToReadableStream`
|
|
54
|
+
interleave them ahead of each pass's html):
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { renderToString } from 'octane/server';
|
|
58
|
+
|
|
59
|
+
const { html, css } = renderToString(App);
|
|
60
|
+
// css: '<style data-octane="sc.<componentId>.<class>">…</style>…'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
On the client, the engine adopts those server chunks at boot — the same
|
|
64
|
+
component renders reuse the adopted rules with no duplicate injection.
|
|
65
|
+
|
|
66
|
+
`ServerStyleSheet` still works as a compatibility wrapper
|
|
67
|
+
(`sheet.collectStyles(...)` + `sheet.getStyleTags()`) for code written against
|
|
68
|
+
the upstream SSR docs; prefer the automatic channel and use one of the two per
|
|
69
|
+
page. `interleaveWithNodeStream` throws — octane streaming already interleaves
|
|
70
|
+
styles.
|
|
71
|
+
|
|
72
|
+
### Architecture
|
|
73
|
+
|
|
74
|
+
CSS compilation and rule delivery are separate layers. The shared sheet owns
|
|
75
|
+
component ordering and content caches; explicit outputs handle browser
|
|
76
|
+
CSSOM/text insertion, Octane's stateless per-request SSR channel, or the
|
|
77
|
+
in-memory capture used by `ServerStyleSheet`. Dynamic interpolations and
|
|
78
|
+
`.attrs` functions still run on every component render, while unchanged
|
|
79
|
+
compiled CSS is deduplicated by content. Static component styles additionally
|
|
80
|
+
reuse their injected class for stable client sheet/Stylis configurations;
|
|
81
|
+
dynamic styles never use that shortcut.
|
|
82
|
+
|
|
83
|
+
## Octane adaptations
|
|
84
|
+
|
|
85
|
+
- `ref` is a plain prop (octane has no `forwardRef`); it always attaches to
|
|
86
|
+
the rendered element and is never filtered by `shouldForwardProp`.
|
|
87
|
+
- `defaultProps` on a styled component is resolved by the factory at render
|
|
88
|
+
time; `styled(Styled)` folding deep-merges as upstream.
|
|
89
|
+
- Polymorphic `as` typing is pragmatic: component targets infer props from
|
|
90
|
+
their function signature; host tags accept a permissive prop bag (octane has
|
|
91
|
+
no `JSX.IntrinsicElements` map).
|
|
92
|
+
- The babel `css` prop transform is not supported.
|
|
93
|
+
- Unnamed stylis plugins throw the documented error 15 (upstream 6.4.3
|
|
94
|
+
constructs that error but forgets to throw it).
|
|
95
|
+
|
|
96
|
+
## Status
|
|
97
|
+
|
|
98
|
+
See the generated [bindings status table](../../docs/bindings-status.md) and
|
|
99
|
+
this package's [status.json](./status.json) for the verified surface and the
|
|
100
|
+
full divergence list.
|
|
101
|
+
|
|
102
|
+
Ported from [styled-components](https://github.com/styled-components/styled-components)
|
|
103
|
+
(MIT © Glen Maddern and contributors).
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/styled-components",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22"
|
|
8
|
+
},
|
|
9
|
+
"octane": {
|
|
10
|
+
"hookSlots": {
|
|
11
|
+
"manual": [
|
|
12
|
+
"src"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"description": "styled-components for the octane renderer — the full v6 web API (styled, css, keyframes, createGlobalStyle, theming, StyleSheetManager, ServerStyleSheet) over octane's ref-as-prop components and its automatic SSR css channel.",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Dominic Gannaway",
|
|
19
|
+
"email": "dg@domgan.com"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
27
|
+
"directory": "packages/styled-components"
|
|
28
|
+
},
|
|
29
|
+
"main": "src/index.ts",
|
|
30
|
+
"module": "src/index.ts",
|
|
31
|
+
"types": "src/index.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"src",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"exports": {
|
|
37
|
+
".": "./src/index.ts"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@emotion/is-prop-valid": "1.4.0",
|
|
41
|
+
"csstype": "3.2.3",
|
|
42
|
+
"stylis": "^4.3.2"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"octane": "0.1.8"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@tsrx/react": "^0.2.37",
|
|
49
|
+
"@types/stylis": "^4.2.5",
|
|
50
|
+
"esbuild": "^0.28.1",
|
|
51
|
+
"react": "^19.2.0",
|
|
52
|
+
"react-dom": "^19.2.0",
|
|
53
|
+
"styled-components": "6.4.3",
|
|
54
|
+
"vitest": "^4.1.9",
|
|
55
|
+
"octane": "0.1.8"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"test": "vitest run"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Ported from styled-components 6.4.3 (MIT). Octane adaptations: no React/RSC
|
|
2
|
+
// probing; adds the octane SSR css-channel chunk prefix.
|
|
3
|
+
declare let SC_DISABLE_SPEEDY: boolean | null | undefined;
|
|
4
|
+
|
|
5
|
+
export const SC_ATTR: string =
|
|
6
|
+
(typeof process !== 'undefined' &&
|
|
7
|
+
typeof process.env !== 'undefined' &&
|
|
8
|
+
(process.env.REACT_APP_SC_ATTR || process.env.SC_ATTR)) ||
|
|
9
|
+
'data-styled';
|
|
10
|
+
|
|
11
|
+
export const SC_ATTR_ACTIVE = 'active';
|
|
12
|
+
export const SC_ATTR_VERSION = 'data-styled-version';
|
|
13
|
+
export const SC_VERSION = '6.4.3';
|
|
14
|
+
export const SPLITTER = '/*!sc*/\n';
|
|
15
|
+
|
|
16
|
+
export const IS_BROWSER = typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
17
|
+
|
|
18
|
+
// Chunk ids emitted into octane's SSR css channel are namespaced `sc.<cid>.<name>`
|
|
19
|
+
// so client-boot rehydration can tell this binding's chunks apart from octane's
|
|
20
|
+
// own scoped-style hashes on the shared `data-octane` attribute.
|
|
21
|
+
export const OCTANE_CHUNK_PREFIX = 'sc.';
|
|
22
|
+
|
|
23
|
+
function readSpeedyFlag(name: string): boolean | undefined {
|
|
24
|
+
if (typeof process !== 'undefined' && typeof process.env !== 'undefined') {
|
|
25
|
+
const val = process.env[name];
|
|
26
|
+
if (val !== undefined && val !== '') {
|
|
27
|
+
return val !== 'false';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const DISABLE_SPEEDY = Boolean(
|
|
34
|
+
typeof SC_DISABLE_SPEEDY === 'boolean'
|
|
35
|
+
? SC_DISABLE_SPEEDY
|
|
36
|
+
: (readSpeedyFlag('REACT_APP_SC_DISABLE_SPEEDY') ??
|
|
37
|
+
readSpeedyFlag('SC_DISABLE_SPEEDY') ??
|
|
38
|
+
(typeof process !== 'undefined' && typeof process.env !== 'undefined'
|
|
39
|
+
? process.env.NODE_ENV !== 'production'
|
|
40
|
+
: false)),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
export const KEYFRAMES_ID_PREFIX = 'sc-keyframes-';
|
|
44
|
+
|
|
45
|
+
// Shared empty execution context when generating static styles
|
|
46
|
+
export const STATIC_EXECUTION_CONTEXT = {};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Attrs,
|
|
3
|
+
BaseObject,
|
|
4
|
+
ExecutionProps,
|
|
5
|
+
Interpolation,
|
|
6
|
+
IStyledComponent,
|
|
7
|
+
IStyledComponentFactory,
|
|
8
|
+
KnownTarget,
|
|
9
|
+
MakeAttrsOptional,
|
|
10
|
+
Runtime,
|
|
11
|
+
StyledOptions,
|
|
12
|
+
StyledTarget,
|
|
13
|
+
Styles,
|
|
14
|
+
Substitute,
|
|
15
|
+
TargetProps,
|
|
16
|
+
WidenUntypedProps,
|
|
17
|
+
} from '../types';
|
|
18
|
+
import { EMPTY_OBJECT } from '../utils/empties';
|
|
19
|
+
import styledError from '../utils/error';
|
|
20
|
+
import css from './css';
|
|
21
|
+
|
|
22
|
+
type AttrsResult<T extends Attrs<any>> = T extends (...args: any) => infer P
|
|
23
|
+
? P extends object
|
|
24
|
+
? P
|
|
25
|
+
: never
|
|
26
|
+
: T extends object
|
|
27
|
+
? T
|
|
28
|
+
: never;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Based on Attrs being a simple object or function that returns
|
|
32
|
+
* a prop object, inspect the attrs result and attempt to extract
|
|
33
|
+
* any "as" prop usage to modify the runtime target.
|
|
34
|
+
*/
|
|
35
|
+
type AttrsTarget<
|
|
36
|
+
R extends Runtime,
|
|
37
|
+
T extends Attrs<any>,
|
|
38
|
+
FallbackTarget extends StyledTarget<R>,
|
|
39
|
+
Result extends ExecutionProps = AttrsResult<T>,
|
|
40
|
+
> = Result extends { as: infer RuntimeTarget }
|
|
41
|
+
? RuntimeTarget extends KnownTarget
|
|
42
|
+
? RuntimeTarget
|
|
43
|
+
: FallbackTarget
|
|
44
|
+
: FallbackTarget;
|
|
45
|
+
|
|
46
|
+
export interface Styled<
|
|
47
|
+
out R extends Runtime,
|
|
48
|
+
out Target extends StyledTarget<R>,
|
|
49
|
+
in out OuterProps extends object,
|
|
50
|
+
out OuterStatics extends object = BaseObject,
|
|
51
|
+
out AttrsKeys extends keyof any = never,
|
|
52
|
+
> {
|
|
53
|
+
<Props extends object = BaseObject, Statics extends object = BaseObject>(
|
|
54
|
+
initialStyles: Styles<Substitute<OuterProps, NoInfer<Props>>>,
|
|
55
|
+
...interpolations: Interpolation<Substitute<OuterProps, NoInfer<Props>>>[]
|
|
56
|
+
): IStyledComponent<R, MakeAttrsOptional<Substitute<OuterProps, Props>, AttrsKeys>> &
|
|
57
|
+
OuterStatics &
|
|
58
|
+
Statics &
|
|
59
|
+
// Surface the wrapped component's hoisted statics on the styled type
|
|
60
|
+
// (octane adaptation of upstream's `Omit<Target, keyof React.Component>`).
|
|
61
|
+
(R extends 'web'
|
|
62
|
+
? Target extends string
|
|
63
|
+
? {}
|
|
64
|
+
: Omit<Target, keyof Function | 'displayName' | 'defaultProps'>
|
|
65
|
+
: {});
|
|
66
|
+
|
|
67
|
+
attrs: <
|
|
68
|
+
Props extends object = BaseObject,
|
|
69
|
+
PrivateMergedProps extends object = Substitute<OuterProps, Props>,
|
|
70
|
+
// Widen when the merged props are un-introspectable ({}) so attrs can backfill
|
|
71
|
+
// arbitrary keys on e.g. Mantine polymorphic-factory targets, matching the
|
|
72
|
+
// permissive JSX call site. Targets with known props are unaffected.
|
|
73
|
+
PrivateAttrsArg extends Attrs<WidenUntypedProps<PrivateMergedProps>> = Attrs<
|
|
74
|
+
WidenUntypedProps<PrivateMergedProps>
|
|
75
|
+
>,
|
|
76
|
+
PrivateResolvedTarget extends StyledTarget<R> = AttrsTarget<R, PrivateAttrsArg, Target>,
|
|
77
|
+
>(
|
|
78
|
+
attrs: PrivateAttrsArg,
|
|
79
|
+
) => Styled<
|
|
80
|
+
R,
|
|
81
|
+
PrivateResolvedTarget,
|
|
82
|
+
PrivateResolvedTarget extends KnownTarget
|
|
83
|
+
? Substitute<Substitute<OuterProps, TargetProps<PrivateResolvedTarget>>, Props>
|
|
84
|
+
: PrivateMergedProps,
|
|
85
|
+
OuterStatics,
|
|
86
|
+
AttrsKeys | keyof AttrsResult<PrivateAttrsArg>
|
|
87
|
+
>;
|
|
88
|
+
|
|
89
|
+
withConfig: (
|
|
90
|
+
config: StyledOptions<R, OuterProps>,
|
|
91
|
+
) => Styled<R, Target, OuterProps, OuterStatics, AttrsKeys>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default function constructWithOptions<
|
|
95
|
+
R extends Runtime,
|
|
96
|
+
Target extends StyledTarget<R>,
|
|
97
|
+
OuterProps extends object = Target extends KnownTarget ? TargetProps<Target> : BaseObject,
|
|
98
|
+
OuterStatics extends object = BaseObject,
|
|
99
|
+
AttrsKeys extends keyof any = never,
|
|
100
|
+
>(
|
|
101
|
+
componentConstructor: IStyledComponentFactory<R, StyledTarget<R>, object, any>,
|
|
102
|
+
tag: StyledTarget<R>,
|
|
103
|
+
options: StyledOptions<R, OuterProps> = EMPTY_OBJECT,
|
|
104
|
+
): Styled<R, Target, OuterProps, OuterStatics, AttrsKeys> {
|
|
105
|
+
/**
|
|
106
|
+
* We trust that the tag is a valid component as long as it isn't
|
|
107
|
+
* falsish. Typically the tag here is a string or function (i.e.
|
|
108
|
+
* class or pure function component), however a component may also be
|
|
109
|
+
* an object if it uses another utility, e.g. React.memo. React will
|
|
110
|
+
* output an appropriate warning however if the `tag` isn't valid.
|
|
111
|
+
*/
|
|
112
|
+
if (!tag) {
|
|
113
|
+
throw styledError(1, tag);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* This is callable directly as a template function */
|
|
117
|
+
const templateFunction = <Props extends object = BaseObject, Statics extends object = BaseObject>(
|
|
118
|
+
initialStyles: Styles<Substitute<OuterProps, Props>>,
|
|
119
|
+
...interpolations: Interpolation<Substitute<OuterProps, Props>>[]
|
|
120
|
+
) =>
|
|
121
|
+
componentConstructor<Substitute<OuterProps, Props>, Statics>(
|
|
122
|
+
tag,
|
|
123
|
+
options as StyledOptions<R, Substitute<OuterProps, Props>>,
|
|
124
|
+
css<Substitute<OuterProps, Props>>(initialStyles, ...interpolations),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Attrs allows for accomplishing two goals:
|
|
129
|
+
*
|
|
130
|
+
* 1. Backfilling props at runtime more expressively than defaultProps
|
|
131
|
+
* 2. Amending the prop interface of a wrapped styled component
|
|
132
|
+
*/
|
|
133
|
+
templateFunction.attrs = <
|
|
134
|
+
Props extends object = BaseObject,
|
|
135
|
+
PrivateMergedProps extends object = Substitute<OuterProps, Props>,
|
|
136
|
+
PrivateAttrsArg extends Attrs<WidenUntypedProps<PrivateMergedProps>> = Attrs<
|
|
137
|
+
WidenUntypedProps<PrivateMergedProps>
|
|
138
|
+
>,
|
|
139
|
+
PrivateResolvedTarget extends StyledTarget<R> = AttrsTarget<R, PrivateAttrsArg, Target>,
|
|
140
|
+
>(
|
|
141
|
+
attrs: PrivateAttrsArg,
|
|
142
|
+
) =>
|
|
143
|
+
constructWithOptions<
|
|
144
|
+
R,
|
|
145
|
+
PrivateResolvedTarget,
|
|
146
|
+
PrivateResolvedTarget extends KnownTarget
|
|
147
|
+
? Substitute<Substitute<OuterProps, TargetProps<PrivateResolvedTarget>>, Props>
|
|
148
|
+
: PrivateMergedProps,
|
|
149
|
+
OuterStatics,
|
|
150
|
+
AttrsKeys | keyof AttrsResult<PrivateAttrsArg>
|
|
151
|
+
>(componentConstructor, tag, {
|
|
152
|
+
...options,
|
|
153
|
+
attrs: Array.prototype.concat(options.attrs, attrs).filter(Boolean),
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* If config methods are called, wrap up a new template function
|
|
158
|
+
* and merge options.
|
|
159
|
+
*/
|
|
160
|
+
templateFunction.withConfig = (config: StyledOptions<R, OuterProps>) =>
|
|
161
|
+
constructWithOptions<R, Target, OuterProps, OuterStatics, AttrsKeys>(
|
|
162
|
+
componentConstructor,
|
|
163
|
+
tag,
|
|
164
|
+
{
|
|
165
|
+
...options,
|
|
166
|
+
...config,
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
return templateFunction;
|
|
171
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Ported from styled-components 6.4.3 (MIT), adapted for octane:
|
|
2
|
+
// - Server rendering is stateless and content-addressed. The output backend
|
|
3
|
+
// forwards rules to octane's active request (and optionally captures a copy
|
|
4
|
+
// for ServerStyleSheet compatibility).
|
|
5
|
+
// - On the client, the first layout effect additionally adopts-by-replacement:
|
|
6
|
+
// it removes the server-emitted css-channel tags for this component right
|
|
7
|
+
// after inserting the equivalent rules into the client engine (pre-paint).
|
|
8
|
+
import { memo, useContext, useLayoutEffect, useRef } from 'octane';
|
|
9
|
+
|
|
10
|
+
import { STATIC_EXECUTION_CONTEXT } from '../constants';
|
|
11
|
+
import GlobalStyle from '../models/GlobalStyle';
|
|
12
|
+
import { useStyleSheetContext } from '../models/StyleSheetManager';
|
|
13
|
+
import { DefaultTheme, ThemeContext } from '../models/ThemeProvider';
|
|
14
|
+
import StyleSheet from '../sheet';
|
|
15
|
+
import { removeServerGlobalTags } from '../sheet/octaneChannel';
|
|
16
|
+
import { ExecutionContext, ExecutionProps, Interpolation, Stringifier, Styles } from '../types';
|
|
17
|
+
import { checkDynamicCreation } from '../utils/checkDynamicCreation';
|
|
18
|
+
import determineTheme from '../utils/determineTheme';
|
|
19
|
+
import generateComponentId from '../utils/generateComponentId';
|
|
20
|
+
import css from './css';
|
|
21
|
+
|
|
22
|
+
const SLOT_GS_INSTANCE = Symbol.for('@octanejs/styled-components:gs-instance');
|
|
23
|
+
const SLOT_GS_PREV = Symbol.for('@octanejs/styled-components:gs-prev');
|
|
24
|
+
const SLOT_GS_UPDATE = Symbol.for('@octanejs/styled-components:gs-update');
|
|
25
|
+
const SLOT_GS_CLEANUP = Symbol.for('@octanejs/styled-components:gs-cleanup');
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a component that injects global CSS when mounted. Supports theming and dynamic props.
|
|
29
|
+
*
|
|
30
|
+
* ```tsx
|
|
31
|
+
* const GlobalStyle = createGlobalStyle`
|
|
32
|
+
* body { margin: 0; font-family: system-ui; }
|
|
33
|
+
* `;
|
|
34
|
+
* // Render <GlobalStyle /> at the root of your app
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export default function createGlobalStyle<Props extends object>(
|
|
38
|
+
strings: Styles<Props>,
|
|
39
|
+
...interpolations: Array<Interpolation<Props>>
|
|
40
|
+
) {
|
|
41
|
+
const rules = css<Props>(strings, ...interpolations);
|
|
42
|
+
const styledComponentId = `sc-global-${generateComponentId(JSON.stringify(rules))}`;
|
|
43
|
+
const globalStyle = new GlobalStyle<Props>(rules, styledComponentId);
|
|
44
|
+
|
|
45
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
46
|
+
checkDynamicCreation(styledComponentId);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const GlobalStyleComponent = (props: ExecutionProps & Props, _scope?: any) => {
|
|
50
|
+
const ssc = useStyleSheetContext();
|
|
51
|
+
const theme = useContext(ThemeContext);
|
|
52
|
+
|
|
53
|
+
if (process.env.NODE_ENV !== 'production' && (props as any).children != null) {
|
|
54
|
+
console.warn(
|
|
55
|
+
`The global style component ${styledComponentId} was given child JSX. createGlobalStyle does not render children.`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (
|
|
60
|
+
process.env.NODE_ENV !== 'production' &&
|
|
61
|
+
rules.some((rule) => typeof rule === 'string' && rule.indexOf('@import') !== -1)
|
|
62
|
+
) {
|
|
63
|
+
console.warn(
|
|
64
|
+
`Please do not use @import CSS syntax in createGlobalStyle at this time, as the CSSOM APIs we use in production do not handle it well. Instead, we recommend embedding a typical <link> meta tag in your document <head> section, or adding it manually to your index.html.`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (ssc.styleSheet.server) {
|
|
69
|
+
renderServerStyles(props, ssc.styleSheet, theme, ssc.stylis);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Client mounts need stable identities so multiple global-style instances
|
|
74
|
+
// can update and unmount independently within their shared rule group.
|
|
75
|
+
const instanceRef = useRef<number | null>(null, SLOT_GS_INSTANCE);
|
|
76
|
+
if (instanceRef.current === null) {
|
|
77
|
+
instanceRef.current = ssc.styleSheet.allocateGSInstance(styledComponentId);
|
|
78
|
+
}
|
|
79
|
+
const instance = instanceRef.current;
|
|
80
|
+
|
|
81
|
+
// Split into two effects so cleanup (removeStyles → full rebuildGroup) only
|
|
82
|
+
// fires on actual unmount or sheet/globalStyle swap -- NOT on every prop change.
|
|
83
|
+
//
|
|
84
|
+
// For dynamic globals, `props` is a new reference every render, so the render
|
|
85
|
+
// effect re-runs each render and renderStyles' rulesEqual fast-path skips
|
|
86
|
+
// rebuildGroup when the CSS is unchanged.
|
|
87
|
+
//
|
|
88
|
+
// globalStyle is included in render deps so HMR-induced module re-evaluation
|
|
89
|
+
// (which creates a new GlobalStyle instance) triggers effect re-run.
|
|
90
|
+
const renderDeps = globalStyle.isStatic
|
|
91
|
+
? [instance, ssc.styleSheet, globalStyle]
|
|
92
|
+
: [instance, props, ssc.styleSheet, theme, ssc.stylis, globalStyle];
|
|
93
|
+
|
|
94
|
+
const prevGlobalStyleRef = useRef(globalStyle, SLOT_GS_PREV);
|
|
95
|
+
|
|
96
|
+
useLayoutEffect(
|
|
97
|
+
() => {
|
|
98
|
+
// HMR creates a new globalStyle instance but the componentId stays
|
|
99
|
+
// stable, so stale hasNameForId hits skip injection.
|
|
100
|
+
if (prevGlobalStyleRef.current !== globalStyle) {
|
|
101
|
+
ssc.styleSheet.clearRules(styledComponentId);
|
|
102
|
+
prevGlobalStyleRef.current = globalStyle;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
renderStyles(instance, props, ssc.styleSheet, theme, ssc.stylis);
|
|
106
|
+
// Adopt-by-replacement of the SSR css-channel tags for this component:
|
|
107
|
+
// the equivalent rules are now live in the client engine, and this
|
|
108
|
+
// effect is synchronous pre-paint, so there is no unstyled flash.
|
|
109
|
+
removeServerGlobalTags(styledComponentId);
|
|
110
|
+
},
|
|
111
|
+
renderDeps,
|
|
112
|
+
SLOT_GS_UPDATE,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// Cleanup-only effect: fires on unmount, sheet swap, or HMR globalStyle
|
|
116
|
+
// swap. Closure captures the specific globalStyle/sheet that owned this
|
|
117
|
+
// instance's rules so HMR cleanup targets the prior module's state.
|
|
118
|
+
useLayoutEffect(
|
|
119
|
+
() => {
|
|
120
|
+
return () => {
|
|
121
|
+
globalStyle.removeStyles(instance, ssc.styleSheet);
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
[instance, ssc.styleSheet, globalStyle],
|
|
125
|
+
SLOT_GS_CLEANUP,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return null;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
function renderStyles(
|
|
132
|
+
instance: number,
|
|
133
|
+
props: ExecutionProps,
|
|
134
|
+
styleSheet: StyleSheet,
|
|
135
|
+
theme: DefaultTheme | undefined,
|
|
136
|
+
stylis: Stringifier,
|
|
137
|
+
) {
|
|
138
|
+
globalStyle.renderStyles(instance, resolveExecutionContext(props, theme), styleSheet, stylis);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function renderServerStyles(
|
|
142
|
+
props: ExecutionProps,
|
|
143
|
+
styleSheet: StyleSheet,
|
|
144
|
+
theme: DefaultTheme | undefined,
|
|
145
|
+
stylis: Stringifier,
|
|
146
|
+
) {
|
|
147
|
+
globalStyle.renderServerStyles(resolveExecutionContext(props, theme), styleSheet, stylis);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function resolveExecutionContext(
|
|
151
|
+
props: ExecutionProps,
|
|
152
|
+
theme: DefaultTheme | undefined,
|
|
153
|
+
): ExecutionContext & Props {
|
|
154
|
+
if (globalStyle.isStatic) {
|
|
155
|
+
return STATIC_EXECUTION_CONTEXT as ExecutionContext & Props;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
...props,
|
|
160
|
+
theme: determineTheme(props, theme, (GlobalStyleComponent as any).defaultProps),
|
|
161
|
+
} as ExecutionContext & Props;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return memo(GlobalStyleComponent as any);
|
|
165
|
+
}
|