Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: contextual styled-jsx #721

Merged
merged 12 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

declare module 'react' {
interface StyleHTMLAttributes<T> extends HTMLAttributes<T> {
jsx?: boolean
Expand All @@ -8,10 +10,15 @@ declare module 'react' {
export type StyleRegistry = {
styles(options?: { nonce?: boolean }): JSX.Element
flush(): void
add(props: any): void
remove(props: any): void
}
export function useStyleRegistry(): StyleRegistry
export function StyleRegistry({
children
children,
registry
}: {
children: JSX.Element
children: JSX.Element | React.ReactNode
registry?: StyleRegistry
}): JSX.Element
export function createStyleRegistry(): StyleRegistry
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { StyleRegistry, useStyleRegistry } from './stylesheet-registry'
export {
StyleRegistry,
createStyleRegistry,
useStyleRegistry
} from './stylesheet-registry'
60 changes: 30 additions & 30 deletions src/stylesheet-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import React, { useState, useContext, createContext, useMemo } from 'react'

import DefaultStyleSheet from './lib/stylesheet'
import { computeId, computeSelector } from './lib/hash'

function mapRulesToStyle(cssRules, options = {}) {
return cssRules.map(args => {
const id = args[0]
const css = args[1]
return React.createElement('style', {
id: `__${id}`,
// Avoid warnings upon render with a key
key: `__${id}`,
nonce: options.nonce ? options.nonce : undefined,
dangerouslySetInnerHTML: {
__html: css
}
})
})
}
export class StyleSheetRegistry {
constructor({
styleSheet = null,
Expand Down Expand Up @@ -120,6 +136,10 @@ export class StyleSheetRegistry {
)
}

styles(options) {
return mapRulesToStyle(this.cssRules(), options)
}

getIdAndRules(props) {
const { children: css, dynamic, id } = props

Expand Down Expand Up @@ -163,11 +183,17 @@ function invariant(condition, message) {
}
}

export const StyleSheetContext = createContext(new StyleSheetRegistry())
export const StyleSheetContext = createContext(null)

export function createStyleRegistry() {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
return new StyleSheetRegistry()
}

export function StyleRegistry({ children }) {
export function StyleRegistry({ registry: configuredRegistry, children }) {
const rootRegistry = useContext(StyleSheetContext)
const [registry] = useState(() => rootRegistry || new StyleSheetRegistry())
const [registry] = useState(
() => configuredRegistry || rootRegistry || createStyleRegistry()
huozhi marked this conversation as resolved.
Show resolved Hide resolved
)

return React.createElement(
StyleSheetContext.Provider,
Expand All @@ -179,31 +205,5 @@ export function StyleRegistry({ children }) {
export function useStyleRegistry() {
const registry = useContext(StyleSheetContext)

return useMemo(
() => ({
styles(options) {
return mapRulesToStyle(registry.cssRules(), options)
},
flush() {
registry.flush()
}
}),
[registry]
)
}

function mapRulesToStyle(cssRules, options = {}) {
return cssRules.map(args => {
const id = args[0]
const css = args[1]
return React.createElement('style', {
id: `__${id}`,
// Avoid warnings upon render with a key
key: `__${id}`,
nonce: options.nonce ? options.nonce : undefined,
dangerouslySetInnerHTML: {
__html: css
}
})
})
return useMemo(() => registry, [registry])
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}