Skip to content

Commit

Permalink
feat(tokens): add z-index tokens with fallback values (#32356)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmoura authored Sep 13, 2024
1 parent 62f0ffc commit de8118d
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "feat: add z-index tokens",
"packageName": "@fluentui/tokens",
"email": "marcosvmmoura@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ export const Colors = () => {
// It returns tokens matching the input value.
const searchToken = React.useCallback(
newSearchValue => {
const tokensFoundBySearch = tokens.filter(
token =>
const tokensFoundBySearch = tokens.filter(token => {
const tokensMatchSearchValue = (tokenItem?: string | number) => tokenItem?.toString().includes(newSearchValue);

return (
token.toLowerCase().includes(newSearchValue) ||
theme.webLight[token].toString().includes(newSearchValue) ||
theme.webDark[token].toString().includes(newSearchValue) ||
theme.teamsLight[token].toString().includes(newSearchValue) ||
theme.teamsDark[token].toString().includes(newSearchValue) ||
theme.teamsHighContrast[token].toString().includes(newSearchValue),
);
tokensMatchSearchValue(theme.webLight[token]) ||
tokensMatchSearchValue(theme.webDark[token]) ||
tokensMatchSearchValue(theme.teamsLight[token]) ||
tokensMatchSearchValue(theme.teamsDark[token]) ||
tokensMatchSearchValue(theme.teamsHighContrast[token])
);
});
setTokensSearchResult(tokensFoundBySearch);
},
[setTokensSearchResult],
Expand Down
14 changes: 13 additions & 1 deletion packages/tokens/etc/tokens.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export const teamsHighContrastTheme: Theme;
export const teamsLightTheme: Theme;

// @public (undocumented)
export type Theme = FontSizeTokens & LineHeightTokens & BorderRadiusTokens & StrokeWidthTokens & HorizontalSpacingTokens & VerticalSpacingTokens & DurationTokens & CurveTokens & ShadowTokens & ShadowBrandTokens & FontFamilyTokens & FontWeightTokens & ColorPaletteTokens & ColorStatusTokens & ColorTokens;
export type Theme = FontSizeTokens & LineHeightTokens & BorderRadiusTokens & StrokeWidthTokens & HorizontalSpacingTokens & VerticalSpacingTokens & DurationTokens & CurveTokens & ShadowTokens & ShadowBrandTokens & FontFamilyTokens & FontWeightTokens & ColorPaletteTokens & ColorStatusTokens & ColorTokens & ZIndexTokens;

// @public
export function themeToTokensObject<TTheme extends Theme>(theme: TTheme): Record<keyof TTheme, string>;
Expand Down Expand Up @@ -510,6 +510,18 @@ export const webDarkTheme: Theme;
// @public (undocumented)
export const webLightTheme: Theme;

// @public
export type ZIndexTokens = {
zIndexBackground?: string;
zIndexContent?: string;
zIndexOverlay?: string;
zIndexPopup?: string;
zIndexMessages?: string;
zIndexFloating?: string;
zIndexPriority?: string;
zIndexDebug?: string;
};

// (No @packageDocumentation comment for this package)

```
1 change: 1 addition & 0 deletions packages/tokens/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ export type {
Theme,
TypographyStyle,
TypographyStyles,
ZIndexTokens,
} from './types';
16 changes: 14 additions & 2 deletions packages/tokens/src/themeToTokensObject.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { webLightTheme } from './themes/web/lightTheme';
import { themeToTokensObject } from './themeToTokensObject';
import { tokens } from './tokens';
import { Theme } from './types';

function assertKeys(generatedTokens: Record<keyof Theme, string>, expectedTokens = tokens) {
Object.keys(generatedTokens).forEach(token => {
expect(expectedTokens).toHaveProperty(token);
expect.objectContaining({
[token]: expect.stringMatching(`var\\(--${token}(, .+)?\\)`),
});
});
}

describe('themeToTokensObject', () => {
it('passing any of our default themes to the function generates the default tokens object', () => {
const generatedTokens = themeToTokensObject(webLightTheme);
expect(generatedTokens).toEqual(tokens);

assertKeys(generatedTokens);
});

it('passing a custom theme with custom tokens on top of a default theme generates the correct tokens object', () => {
Expand All @@ -17,6 +28,7 @@ describe('themeToTokensObject', () => {
customColor3: 'var(--customColor3)',
};
const generatedTokens = themeToTokensObject(customTheme);
expect(generatedTokens).toEqual(expectedTokens);

assertKeys(generatedTokens, expectedTokens);
});
});
5 changes: 4 additions & 1 deletion packages/tokens/src/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { tokens } from './tokens';
describe('tokens', () => {
it('CSS variables match expected format', () => {
(Object.keys(tokens) as (keyof Theme)[]).forEach(token => {
expect(tokens[token]).toBe(`var(--${token})`);
const tokenValue = tokens[token];
const tokenRegex = new RegExp(`var\\(--${token}(, .+)?\\)`);

expect(tokenValue).toMatch(tokenRegex);
});
});
});
54 changes: 54 additions & 0 deletions packages/tokens/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,58 @@ export const tokens: Record<keyof Theme, string> = {
curveEasyEaseMax: 'var(--curveEasyEaseMax)',
curveEasyEase: 'var(--curveEasyEase)',
curveLinear: 'var(--curveLinear)',

/**
* ZIndexes
* Special case where the tokens contain default values
* ZIndexes are not mandatory, so they are not included in the theme, but can be used as tokens with default values
*/

/**
* Elevation 0
* Can be used for background elements, like surfaces
*/
zIndexBackground: 'var(--zIndexBackground, 0)',

/**
* Elevation 2
* Can be used content that is on top of the background, like cards
*/
zIndexContent: 'var(--zIndexContent, 1)',

/**
* Elevation 4
* Can be used for overlays, like the backdrop of a modal
*/
zIndexOverlay: 'var(--zIndexOverlay, 1000)',

/**
* Elevation 8
* Can be used for popups, like modals and drawers
*/
zIndexPopup: 'var(--zIndexPopup, 2000)',

/**
* Elevation 16
* Can be used for messages, like snackbars and toasts
*/
zIndexMessages: 'var(--zIndexMessages, 3000)',

/**
* Elevation 28
* Can be used for floating elements, like dropdowns
*/
zIndexFloating: 'var(--zIndexFloating, 4000)',

/**
* Elevation 64
* Can be used for high priority floating elements, like tooltips
*/
zIndexPriority: 'var(--zIndexPriority, 5000)',

/**
* Special elevation
* Can be used for elements that need to be above everything else, like debug overlays
*/
zIndexDebug: 'var(--zIndexDebug, 6000)',
};
17 changes: 16 additions & 1 deletion packages/tokens/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,20 @@ export type CurveTokens = {
curveLinear: string;
};

/**
* Design tokens for z-index groups and levels
*/
export type ZIndexTokens = {
zIndexBackground?: string;
zIndexContent?: string;
zIndexOverlay?: string;
zIndexPopup?: string;
zIndexMessages?: string;
zIndexFloating?: string;
zIndexPriority?: string;
zIndexDebug?: string;
};

/**
* Design tokens for shadow levels
*/
Expand Down Expand Up @@ -773,6 +787,7 @@ export type Theme = FontSizeTokens &
FontWeightTokens &
ColorPaletteTokens &
ColorStatusTokens &
ColorTokens;
ColorTokens &
ZIndexTokens;

export type PartialTheme = Partial<Theme>;

0 comments on commit de8118d

Please sign in to comment.