feat(css): add cssVars prop and resolveCssVars middleware#2966
Open
janpe wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #2853. Supplies CSS custom properties from JS for SVGs that reference
var(--name)without defining the variables inline — useful for theming or when prepending values to an SVG string received from elsewhere.How
Adds two new exports from
react-native-svg/css:resolveCssVars(vars): Middleware— a newMiddlewarethat resolvesvar()references on color-like props (color, fill, floodColor, lightingColor, stopColor, stroke) against a caller-supplied variable map. Reuses the existingcssSelectquery andresolveElementVariableshelpers frominlineStyles.CssVars— the variable map type.The intended composition is to run
inlineStylesfirst andresolveCssVarssecond, so<style>-declared variables win over caller-supplied ones:For convenience,
SvgCss,SvgCssUri,SvgWithCss, andSvgWithCssUriaccept an optionalcssVarsprop that performs the composition internally.Scope
src/css/css.tsx,src/css/index.tsx). No native code.inlineStylesis unchanged. TheMiddlewaretype is unchanged.parse(xml, inlineStyles),<SvgCss xml={...} />withoutcssVars) take exactly the same code path as before.Test Plan
yarn jest css.test— three new tests pass:resolveCssVars resolves var() when no <style> is presentcomposing resolveCssVars over inlineStyles lets <style> winSvgCss renders with cssVars propCompatibility
JS-only change — runs everywhere the existing
SvgCsscomponents run.Checklist
USAGE.md(the project's user-facing docs;README.mdlinks here)__tests__folder