Skip to content

Commit

Permalink
Merge pull request #1986 from tf/storybook-theme-property-scopes
Browse files Browse the repository at this point in the history
Support scopes in storybook theme properties
  • Loading branch information
tf authored Aug 17, 2023
2 parents b2cb178 + e26449d commit f4e26a1
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 36 deletions.
14 changes: 11 additions & 3 deletions entry_types/scrolled/package/spec/support/__spec__/stories-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ describe('exampleStories', () => {
name: 'With Properties',
themeOptions: {
properties: {
accentColor: 'red'
root: {
accentColor: 'red'
}
}
}
}]
Expand All @@ -91,13 +93,19 @@ describe('exampleStories', () => {
theme: expect.objectContaining({
options: expect.objectContaining({
properties: expect.objectContaining({
accentColor: 'red'
root: {
accentColor: 'red'
}
})
})
})
})
}),
cssProperties: {'--theme-accent-color': 'red'}
cssRules: {
root: {
'--theme-accent-color': 'red'
}
}
}));
});

Expand Down
37 changes: 31 additions & 6 deletions entry_types/scrolled/package/spec/support/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ let seedFixture = seedFixtureFromFile;
* configuration: {size: 'large'},
* themeOptions: {
* properties: {
* accentColor: '#0f0'
* root: {
* accentColor: '#0f0'
* }
* }
* }
* }
Expand All @@ -69,7 +71,7 @@ export function storiesOfContentElement(module, options) {
}

exampleStories(options).forEach(({
title, seed, requireConsentOptIn, cssProperties, parameters = {}
title, seed, requireConsentOptIn, cssRules, parameters = {}
}) => {
const consent = Consent.create();

Expand All @@ -83,9 +85,10 @@ export function storiesOfContentElement(module, options) {
stories.add(title,
() =>
<RootProviders seed={seed} consent={consent}>
<div style={cssProperties}>
<Entry />
</div>
<style>
{renderCss(cssRules)}
</style>
<Entry />
</RootProviders>,
{
...parameters,
Expand All @@ -97,6 +100,22 @@ export function storiesOfContentElement(module, options) {
});
}

function renderCss(rules) {
return Object.entries(rules).map(([name, properties]) => {
const selector = (name === 'root' ? ':root' : `.scope-${name}`);

const declations = Object.entries(properties).map(([key, value]) =>
`${key}: ${value};`
).join('\n');

return `
${selector} {
${declations}
}
`
}).join('\n');
}

/**
* When generating a `.storybook/seed.json` via the
* `pageflow_scrolled:storybook:seed:setup` Rake task, a couple of
Expand Down Expand Up @@ -259,7 +278,13 @@ function exampleStoryGroup({name, typeName, examples, parameters, requireConsent
}),
requireConsentOptIn,
parameters,
cssProperties: themeCssProperties(examples[index].themeOptions?.properties || {})
cssRules: Object.entries(examples[index].themeOptions?.properties || {}).reduce(
(result, [scope, properties]) => {
result[scope] = themeCssProperties(properties);
return result;
},
{}
)
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ storiesOfContentElement(module, {
name: 'Pallete number color',
themeOptions: {
properties: {
paletteColorAccent: '#04f'
root: {
paletteColorAccent: '#04f'
}
}
},
configuration: {
Expand Down
26 changes: 17 additions & 9 deletions entry_types/scrolled/package/src/contentElements/quote/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ storiesOfContentElement(module, {
name: 'Centered Attribution',
themeOptions: {
properties: {
quoteAttributionMinWidth: '50%'
root: {
quoteAttributionMinWidth: '50%'
}
}
}
},
Expand All @@ -65,28 +67,34 @@ storiesOfContentElement(module, {
themeOptions: {
quoteDesign: 'inline',
properties: {
quoteLeftMark: '"»"',
quoteRightMark: '"«"',
quoteMarkFontWeight: 'normal',
quoteIndent: 0,
quoteMarkOpacity: 1
root: {
quoteLeftMark: '"»"',
quoteRightMark: '"«"',
quoteMarkFontWeight: 'normal',
quoteIndent: 0,
quoteMarkOpacity: 1
}
}
}
},
{
name: 'Double angle',
themeOptions: {
properties: {
quoteLeftMark: '"»"',
quoteLeftMarkTop: '-0.35em'
root: {
quoteLeftMark: '"»"',
quoteLeftMarkTop: '-0.35em'
}
}
}
},
{
name: 'Pallete color',
themeOptions: {
properties: {
paletteColorAccent: '#04f'
root: {
paletteColorAccent: '#04f'
}
}
},
configuration: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ storiesOfContentElement(module, {
configuration: linkExampleConfiguration,
themeOptions: {
properties: {
contentLinkColor: 'red'
root: {
contentLinkColor: 'red'
}
}
}
},
Expand All @@ -120,8 +122,10 @@ storiesOfContentElement(module, {
configuration: linkExampleConfiguration,
themeOptions: {
properties: {
lightContentLinkColor: 'yellow',
darkContentLinkColor: 'green',
root: {
lightContentLinkColor: 'yellow',
darkContentLinkColor: 'green',
}
}
}
},
Expand All @@ -133,8 +137,10 @@ storiesOfContentElement(module, {
},
themeOptions: {
properties: {
lightContentLinkColor: 'yellow',
darkContentLinkColor: 'green',
root: {
lightContentLinkColor: 'yellow',
darkContentLinkColor: 'green',
}
}
}
},
Expand All @@ -143,10 +149,12 @@ storiesOfContentElement(module, {
themeOptions: {
quoteDesign: 'hanging',
properties: {
quoteLeftMark: '"»"',
quoteRightMark: '"«"',
quoteMarkOpacity: 1,
quoteMarkFontWeight: 'normal'
root: {
quoteLeftMark: '"»"',
quoteRightMark: '"«"',
quoteMarkOpacity: 1,
quoteMarkFontWeight: 'normal'
}
}
}
},
Expand All @@ -155,21 +163,25 @@ storiesOfContentElement(module, {
themeOptions: {
quoteDesign: 'inline',
properties: {
quoteLeftMark: '"»"',
quoteRightMark: '"«"'
root: {
quoteLeftMark: '"»"',
quoteRightMark: '"«"'
}
}
}
},
{
name: 'With custom lists',
themeOptions: {
properties: {
textBlockUnorderedListStyleType: '"- "',
textBlockUnorderedListMarkerColor: 'red',
textBlockUnorderedListIndent: '15px',
textBlockOrderedListIndent: '60px',
textBlockFirstListItemMarginTop: '2rem',
textBlockListItemMarginTop: 0
root: {
textBlockUnorderedListStyleType: '"- "',
textBlockUnorderedListMarkerColor: 'red',
textBlockUnorderedListIndent: '15px',
textBlockOrderedListIndent: '60px',
textBlockFirstListItemMarginTop: '2rem',
textBlockListItemMarginTop: 0
}
}
}
}
Expand Down

0 comments on commit f4e26a1

Please sign in to comment.