-
Notifications
You must be signed in to change notification settings - Fork 46
Feat/scoped variables #611
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
Open
dlebedynskyi
wants to merge
7
commits into
uni-stack:main
Choose a base branch
from
dlebedynskyi:feat/scoped-variables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7066b9d
feat: add ScopedVariables component for per-subtree CSS variable over…
6fefdd7
feat: add opt-in cacheKey to ScopedVariables for native style caching
8cc0e72
docs(expo-example): add ScopedVariables demo
232702e
fix(ScopedVariables): apply scoped variables to the web DOM cascade
c34ed83
fix(ScopedVariables): address review feedback on PR #611
d21795e
refactor(ScopedVariables): address maintainer review on PR #611
367cadc
fix(ScopedVariables): use JSON.stringify for the derived cache key
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/uniwind/src/components/ScopedVariables/ScopedVariables.native.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import React, { useMemo } from 'react' | ||
| import { UniwindContext, useUniwindContext } from '../../core/context' | ||
| import type { UniwindContextType } from '../../core/types' | ||
| import { buildScopedVariablesContext, type ScopedVariablesProps } from './utils' | ||
|
|
||
| export const ScopedVariables: React.FC<React.PropsWithChildren<ScopedVariablesProps>> = ({ variables, children }) => { | ||
| const uniwindContext = useUniwindContext() | ||
| const value = useMemo<UniwindContextType>( | ||
| () => buildScopedVariablesContext(uniwindContext, variables), | ||
| [uniwindContext, variables], | ||
| ) | ||
|
|
||
| return ( | ||
| <UniwindContext.Provider value={value}> | ||
| {children} | ||
| </UniwindContext.Provider> | ||
| ) | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
packages/uniwind/src/components/ScopedVariables/ScopedVariables.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import React, { useMemo } from 'react' | ||
| import { UniwindContext, useUniwindContext } from '../../core/context' | ||
| import type { UniwindContextType } from '../../core/types' | ||
| import { toWebValue } from '../../core/web/webUtils' | ||
| import { buildScopedVariablesContext, type ScopedVariablesProps } from './utils' | ||
|
|
||
| export const ScopedVariables: React.FC<React.PropsWithChildren<ScopedVariablesProps>> = ({ variables, children }) => { | ||
| const uniwindContext = useUniwindContext() | ||
| const value = useMemo<UniwindContextType>( | ||
| () => buildScopedVariablesContext(uniwindContext, variables), | ||
| [uniwindContext, variables], | ||
| ) | ||
| // Inline custom properties so the DOM cascade resolves var(--name) for descendants | ||
| const style = useMemo<React.CSSProperties>(() => { | ||
| const result: Record<string, string | number> = { display: 'contents' } | ||
|
|
||
| Object.entries(variables).forEach(([name, variableValue]) => { | ||
| if (name.startsWith('--')) { | ||
| result[name] = toWebValue(variableValue) | ||
| } | ||
| }) | ||
|
|
||
| return result as React.CSSProperties | ||
| }, [variables]) | ||
|
|
||
| return ( | ||
| <div style={style}> | ||
| <UniwindContext.Provider value={value}> | ||
| {children} | ||
| </UniwindContext.Provider> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './ScopedVariables' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { Logger } from '../../core/logger' | ||
| import type { CSSVariables, UniwindContextType } from '../../core/types' | ||
|
|
||
| export type ScopedVariablesProps = { | ||
| variables: CSSVariables | ||
| } | ||
|
|
||
| const validateVariables = (variables: CSSVariables) => | ||
| Object.fromEntries( | ||
| Object.entries(variables).filter(([name]) => { | ||
| if (!name.startsWith('--')) { | ||
| if (__DEV__) { | ||
| Logger.error(`CSS variable name must start with "--", instead got: ${name}`) | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| return true | ||
| }), | ||
| ) | ||
|
|
||
| export const buildScopedVariablesContext = ( | ||
| parent: UniwindContextType, | ||
| variables: CSSVariables, | ||
| ): UniwindContextType => { | ||
| // Merged with ancestors, nearest wins | ||
| const mergedVariables = { | ||
| ...parent.variables, | ||
| ...validateVariables(variables), | ||
| } | ||
| const variablesCacheKey = JSON.stringify( | ||
| Object.entries(mergedVariables).sort(([a], [b]) => a.localeCompare(b)), | ||
| ) | ||
|
|
||
| return { | ||
| ...parent, | ||
| variables: mergedVariables, | ||
| variablesCacheKey, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.