diff --git a/.storybook/preview.js b/.storybook/preview.js index 14d3fcbd0..b991cbe37 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.js @@ -3,6 +3,7 @@ import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport'; import ReactSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-light'; import twig from 'react-syntax-highlighter/dist/esm/languages/prism/twig'; import { withTheme } from './theme-decorator'; +import { withTextFlow } from './text-flow-decorator'; import tokens from '../src/compiled/tokens/js/tokens'; import 'focus-visible'; import '../src/index-with-dependencies.scss'; @@ -78,26 +79,72 @@ export const parameters = { }, }; +const directions = ['ltr', 'rtl']; +const writingModes = ['horizontal-tb', 'vertical-lr', 'vertical-rl']; +const textFlowItems = directions + .map((direction) => + writingModes.map((writingMode) => { + return { + value: JSON.stringify({ direction, writingMode }), + title: writingMode, + left: direction, + }; + }) + ) + .flat(); + export const globalTypes = { theme: { name: 'Theme', description: 'Global theme for components', toolbar: { icon: 'paintbrush', + showName: true, items: [ { // 'null' value supports a "no value selected" state, if 'undefined' // there are sometimes missing 'key' errors in console value: null, title: 'No theme', + right: '(default)', + }, + { + value: 't-light', + title: 'Light', + right: '.t-light', }, { value: 't-dark', title: 'Dark', + right: '.t-dark', + }, + { + value: 't-light,t-alternate', + title: 'Light Alt', + right: '.t-light.t-alternate', + }, + { + value: 't-dark,t-alternate', + title: 'Dark Alt', + right: '.t-dark.t-alternate', + }, + ], + }, + }, + textFlow: { + name: 'Text flow', + toolbar: { + icon: 'redirect', + showName: true, + items: [ + { + value: null, + title: 'Default text flow', }, + ...textFlowItems, ], }, }, }; -export const decorators = [withTheme]; +export const decorators = [withTheme, withTextFlow]; diff --git a/.storybook/text-flow-decorator.js b/.storybook/text-flow-decorator.js new file mode 100644 index 000000000..5c66ae9a5 --- /dev/null +++ b/.storybook/text-flow-decorator.js @@ -0,0 +1,57 @@ +import { useEffect } from '@storybook/client-api'; + +const setTextFlow = (element, { direction, writingMode } = {}) => { + if (!element || !element.style) { + return; + } + + if (direction && direction !== 'inherit') { + element.style.setProperty('direction', direction); + } else { + element.style.removeProperty('direction'); + } + + if (writingMode && writingMode !== 'inherit') { + element.style.setProperty('writing-mode', writingMode); + } else { + element.style.removeProperty('writing-mode'); + } +}; + +/** + * Sets text direction and writing-mode for stories to test logical property + * usage. + * + * This unfortunately does not work with non-inline stories in Docs view due to + * some open Storybook issues, specifically + * {@link https://github.com/storybookjs/storybook/issues/14477|#14477} and + * {@link https://github.com/storybookjs/storybook/issues/13444|#13444}. + * + * @param {function} story + * @param {object} context + * @returns {*} Result of story function. + */ +export const withTextFlow = (story, context) => { + let textFlow = context.parameters.textFlow || + context.globals.textFlow || { + direction: 'inherit', + writingMode: 'inherit', + }; + + if (typeof textFlow === 'string') { + textFlow = JSON.parse(textFlow); + } + + if (context.viewMode === 'story') { + useEffect(() => { + setTextFlow(document.body, textFlow); + }); + } else if (context.viewMode === 'docs') { + useEffect(() => { + setTextFlow(document.body); + setTextFlow(document.querySelector(`#story--${context.id}`), textFlow); + }); + } + + return story(); +}; diff --git a/.storybook/theme-decorator.js b/.storybook/theme-decorator.js index cdda41871..fb584a052 100644 --- a/.storybook/theme-decorator.js +++ b/.storybook/theme-decorator.js @@ -23,7 +23,8 @@ const updateTheme = (element, theme) => { } if (theme) { - element.classList.add(theme); + const newThemes = theme.split(','); + element.classList.add(...newThemes); } }; diff --git a/src/utilities/spacing/demo/logical-properties.twig b/src/utilities/spacing/demo/logical-properties.twig deleted file mode 100644 index 7e926f1ae..000000000 --- a/src/utilities/spacing/demo/logical-properties.twig +++ /dev/null @@ -1,27 +0,0 @@ -{% set directions = ['ltr', 'rtl'] %} -{% set writing_modes = ['horizontal-tb', 'vertical-lr', 'vertical-rl'] %} -{% if step < 0 %} - {% set step = 'n' ~ step|abs %} -{% endif %} - -