
Security News
Static vs. Runtime Reachability: Insights from Latio’s On the Record Podcast
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
@fluentui-react-native/use-tokens
Advanced tools
Utilities and hook function for getting themed tokens for a component
This package provides the means to create a hook function to build a set of tokens from a passed in theme as well as a cache object unique to the pairing of the given hook function and theme. These resolved tokens should then be used to build the styles for your component. This way the look and feel of a component can be built up from constants, theme globals, and even component specific overrides.
A token is simply a setting that informs the styling of the component. These can be thought of as an additional set of component props that are automatically built-up by the framework. Typically these are set at component creation time or overriden by a theme but can can come from a variety of sources:
At the core of this system is the idea that: Tokens + Theme ==> Styles
. This can be thought of as:
useMemo
hook or its equivalentThe overall signature of the method is as follows:
export type StyledTextTokens = Pick<TextProps[style], 'color', 'fontFamily', 'fontWeight', 'fontSize'>;
const useTokens = buildUseTokens<StyledTextTokens, Theme>(
/* function for looking up a component in a theme, allows this to be used independently of the shape of the theme */
getComponentInfoFromTheme,
/* Start of tokens, these are applied in order */
/* object, these are effectively constants to be used as defaults */
{ token1: 'foo', token2: 400 },
/* theme function */
(theme: Theme) => ({ token3: theme.colors.backgroundColor }),
/* name to lookup */
'NameToLookup',
);
This useTokens
hook can then be used to build styles for the component as follows:
const StyledText: React.FunctionComponent<TextProps> = (props) => {
// split the props
const { style, children, ...rest } = props;
// left out of the hook to allow theme to be obtained in whatever way is appropriate. Generally from a context.
const theme = useTheme();
// get a set of tokens from the theme
const [tokens, cache] = useTokens(theme);
// create a style object from the tokens using the cache. Note that if all the tokens are actually directly styles the tokens could be used
// directly. This is just to illustrate the pattern for a more complex component
const [tokenStyle] = cache(() => ({ ...tokens }), []);
// now merge any styles coming from the props in on top as they should override
const mergedStyles = mergeStyles(tokenStyle, style);
// now just render the text element
return (
<Text {...rest} style={mergedStyles}>
{children}
</Text>
);
};
These tokens will be deeply merged in the order they appear in the array.
const mergedTokens = merge(
...options.tokens.map(entry => {
// string gets converted to object or function (or empty object)
if (typeof entry === 'string') {
entry = themeHelper.getComponentInfo(entry) || {};
}
// return either the function call (which will produce an object) or the object
return typeof entry === 'function' ? entry(theme) : entry;
});
);
FAQs
Utilities and hook function for getting themed tokens for a component
The npm package @fluentui-react-native/use-tokens receives a total of 660 weekly downloads. As such, @fluentui-react-native/use-tokens popularity was classified as not popular.
We found that @fluentui-react-native/use-tokens demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.