-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace theme add-on with toolbar and global args #1219
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a3ea4aa
Uninstall storybook-addon-themes
tylersticka 4d54454
Add custom theme toolbar
tylersticka d401906
Account for changing tabs with theme selected
tylersticka 776b981
Remove old fill-canvas theme preview technique
tylersticka 52bf4ff
Possibly fix theme decorator issues
tylersticka 2350e69
Theme body instead of rootElement
tylersticka 211c439
Overtly specify light theme
tylersticka 1e813ca
Note unresolved issue in code comments
tylersticka File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useEffect } from '@storybook/client-api'; | ||
|
||
/** | ||
* Removes current theme classes on an element and applies a new theme class | ||
* if provided. | ||
* @param {HTMLElement} element - Element to add/remove theme classes from | ||
* @param {string} [theme] - New theme to apply. | ||
*/ | ||
const updateTheme = (element, theme) => { | ||
if (!element || !element.classList) { | ||
return; | ||
} | ||
|
||
const themes = []; | ||
element.classList.forEach((className) => { | ||
if (className.startsWith('t-') && className !== theme) { | ||
themes.push(className); | ||
} | ||
}); | ||
|
||
if (themes.length > 0) { | ||
element.classList.remove(...themes); | ||
} | ||
|
||
if (theme) { | ||
element.classList.add(theme); | ||
} | ||
}; | ||
|
||
/** | ||
* Adds theme class to containing story elements. | ||
* | ||
* 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 withTheme = (story, context) => { | ||
const theme = context.parameters.theme || context.globals.theme; | ||
|
||
if (context.viewMode === 'story') { | ||
// In canvas view, theme the root HTML element | ||
useEffect(() => { | ||
updateTheme(document.body, theme); | ||
}); | ||
} else if (context.viewMode === 'docs') { | ||
useEffect(() => { | ||
// Remove any existing theme classes from the root element | ||
updateTheme(document.body); | ||
// Query for the most appropriate story element | ||
const storyElement = document.querySelector(`#story--${context.id}`); | ||
// Only proceed if we actually found the relevant story. | ||
if (storyElement) { | ||
const previewElement = storyElement.closest('.docs-story'); | ||
updateTheme(previewElement, theme); | ||
} | ||
}); | ||
} | ||
|
||
return story(); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
{% extends '@cloudfour/design/demo/fill-canvas.twig' %} | ||
|
||
{% block content %} | ||
{% embed '@cloudfour/objects/rhythm/rhythm.twig' with { | ||
class: 'o-rhythm--generous' | ||
} %} | ||
{% block content %} | ||
<h1>t-{{theme}}</h1> | ||
{% include '@cloudfour/design/typography/demo/inline-elements.twig' only %} | ||
{% include '@cloudfour/components/button/demo/styles.twig' only %} | ||
{# TODO: Replace padding and corner modifiers once card component can | ||
handle these variations natively #} | ||
{% set _card_class = 'c-card--horizontal@m u-pad-1 u-rounded' %} | ||
{% if theme == 'light' %} | ||
{% set _card_class = _card_class ~ ' t-dark' %} | ||
{% else %} | ||
{% set _card_class = _card_class ~ ' t-light' %} | ||
{% endif %} | ||
{% include '@cloudfour/components/card/demo/single.twig' with { | ||
class: _card_class, | ||
href: '#', | ||
show_heading: true, | ||
show_content: true, | ||
show_cover: true, | ||
show_footer: true | ||
} only %} | ||
{% endblock %} | ||
{% endembed %} | ||
{% endblock %} | ||
{% embed '@cloudfour/objects/rhythm/rhythm.twig' with { | ||
class: 'o-rhythm--generous' | ||
} %} | ||
{% block content %} | ||
<h1>t-{{theme}}</h1> | ||
{% include '@cloudfour/design/typography/demo/inline-elements.twig' only %} | ||
{% include '@cloudfour/components/button/demo/styles.twig' only %} | ||
{# TODO: Replace padding and corner modifiers once card component can | ||
handle these variations natively #} | ||
{% set _card_class = 'c-card--horizontal@m u-pad-1 u-rounded' %} | ||
{% if theme == 'light' %} | ||
{% set _card_class = _card_class ~ ' t-dark' %} | ||
{% else %} | ||
{% set _card_class = _card_class ~ ' t-light' %} | ||
{% endif %} | ||
{% include '@cloudfour/components/card/demo/single.twig' with { | ||
class: _card_class, | ||
href: '#', | ||
show_heading: true, | ||
show_content: true, | ||
show_cover: true, | ||
show_footer: true | ||
} only %} | ||
{% endblock %} | ||
{% endembed %} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't yet have an understanding of story elements and their contents. Is it a guaranty that there will always exist a
.docs-story
or do we need to check thatpreviewElement
is notnull
before attempting to update the theme? 🤔 ¯\_(ツ)_/¯There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a good safety check given that the underlying Storybook markup is outside our control
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
updateTheme
function does check that the element is truthy and has aclassList
property, so I don't think another check before calling it is necessary.