-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 3543-remove-global-styles
- Loading branch information
Showing
13 changed files
with
291 additions
and
38 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@swisspost/design-system-documentation': minor | ||
--- | ||
|
||
Added a toolbar for switching the theme, channel, and mode of all stories. |
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,39 @@ | ||
@use '@swisspost/design-system-styles/core' as post; | ||
|
||
.addon-dropdown { | ||
min-width: 12rem; | ||
display: flex; | ||
flex-flow: column nowrap; | ||
gap: post.$size-line; | ||
position: absolute; | ||
top: -5px; | ||
right: 0; | ||
padding: post.$size-mini; | ||
background-color: var(--post-light); | ||
border: post.$border-width solid post.$border-color; | ||
border-radius: post.$border-radius; | ||
font-size: post.$font-size-sm; | ||
|
||
.addon-dropdown__item { | ||
display: block; | ||
padding: post.$size-mini post.$size-small-regular; | ||
border-radius: post.$border-radius-sm; | ||
text-decoration: none; | ||
color: post.$body-color !important; | ||
|
||
&:hover { | ||
background-color: post.$gray-10; | ||
} | ||
|
||
&.active { | ||
background-color: post.$yellow; | ||
} | ||
} | ||
} | ||
|
||
.addon-button { | ||
post-icon { | ||
font-size: post.$font-size-20; | ||
margin-inline-end: post.$size-line; | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
packages/documentation/.storybook/addons/styles-switcher/StylesSwitcher.tsx
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,172 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { IconButton, WithTooltip } from '@storybook/components'; | ||
|
||
const STYLESHEET_ID = 'preview-stylesheet'; | ||
const STORAGE_KEY_PREFIX = 'swisspost-documentation'; | ||
const THEMES = ['Post']; | ||
const CHANNELS = ['External', 'Internal']; | ||
const MODES = ['Light', 'Dark']; | ||
|
||
function StylesSwitcher() { | ||
const [currentTheme, setCurrentTheme] = useState<string>( | ||
localStorage.getItem(`${STORAGE_KEY_PREFIX}-theme`) || THEMES[0], | ||
); | ||
const [currentChannel, setCurrentChannel] = useState<string>( | ||
localStorage.getItem(`${STORAGE_KEY_PREFIX}-channel`) || CHANNELS[0], | ||
); | ||
const [currentMode, setCurrentMode] = useState<string>( | ||
localStorage.getItem(`${STORAGE_KEY_PREFIX}-mode`) || MODES[0], | ||
); | ||
|
||
/** | ||
* Sets the 'data-color-mode' attribute and preview stylesheet when the addon initializes | ||
*/ | ||
useEffect(() => { | ||
setPreviewStylesheet(); | ||
setDataColorModeAttribute(); | ||
}); | ||
|
||
/** | ||
* Sets the stylesheet matching the selected theme and channel in the preview document head | ||
*/ | ||
const setPreviewStylesheet = () => { | ||
const preview = getPreviewDocument(); | ||
const previewHead = preview && preview.querySelector('head'); | ||
|
||
if (!previewHead) return; | ||
|
||
let stylesheetLink = previewHead.querySelector(`#${STYLESHEET_ID}`); | ||
|
||
if (!stylesheetLink) { | ||
stylesheetLink = document.createElement('link'); | ||
stylesheetLink.setAttribute('rel', 'stylesheet'); | ||
stylesheetLink.setAttribute('id', STYLESHEET_ID); | ||
previewHead.appendChild(stylesheetLink); | ||
} | ||
|
||
stylesheetLink.setAttribute( | ||
'href', | ||
`/styles/${currentTheme.toLowerCase()}-${currentChannel.toLowerCase()}.css`, | ||
); | ||
}; | ||
|
||
/** | ||
* Sets the 'data-color-mode' attribute of the preview body to match the selected mode | ||
*/ | ||
const setDataColorModeAttribute = () => { | ||
const preview = getPreviewDocument(); | ||
if (!preview) return; | ||
|
||
const mode = currentMode.toLowerCase(); | ||
const storyContainers = preview.querySelectorAll('.sbdocs-preview, .sb-main-padded'); | ||
storyContainers.forEach(storyContainer => { | ||
storyContainer.classList.remove('bg-light', 'bg-dark'); | ||
storyContainer.classList.add(`bg-${mode}`); | ||
storyContainer.setAttribute('data-color-mode', mode); | ||
}); | ||
}; | ||
|
||
/** | ||
* Returns the Document contained in the preview iframe | ||
*/ | ||
const getPreviewDocument = (): Document | undefined => { | ||
const preview = document.querySelector('#storybook-preview-iframe'); | ||
return preview && (preview as HTMLIFrameElement).contentWindow.document; | ||
}; | ||
|
||
/** | ||
* Applies selected theme and registers it to the local storage | ||
*/ | ||
const applyTheme = (theme: string) => { | ||
setCurrentTheme(theme); | ||
localStorage.setItem(`${STORAGE_KEY_PREFIX}-theme`, theme); | ||
}; | ||
|
||
/** | ||
* Applies selected channel and registers it to the local storage | ||
*/ | ||
const applyChannel = (channel: string) => { | ||
setCurrentChannel(channel); | ||
localStorage.setItem(`${STORAGE_KEY_PREFIX}-channel`, channel); | ||
}; | ||
|
||
/** | ||
* Applies selected mode and registers it to the local storage | ||
*/ | ||
const applyMode = (mode: string) => { | ||
setCurrentMode(mode); | ||
localStorage.setItem(`${STORAGE_KEY_PREFIX}-mode`, mode); | ||
}; | ||
|
||
return ( | ||
<> | ||
{/* Theme dropdown */} | ||
<WithTooltip | ||
placement="bottom-end" | ||
trigger="click" | ||
closeOnOutsideClick | ||
tooltip={ | ||
<div className="addon-dropdown"> | ||
{THEMES.map(theme => ( | ||
<IconButton | ||
className={'addon-dropdown__item' + (theme === currentTheme ? ' active' : '')} | ||
key={theme} | ||
onClick={() => applyTheme(theme)} | ||
> | ||
{theme} | ||
</IconButton> | ||
))} | ||
</div> | ||
} | ||
> | ||
<IconButton size="medium">Theme: {currentTheme}</IconButton> | ||
</WithTooltip> | ||
|
||
{/* Channel dropdown */} | ||
<WithTooltip | ||
placement="bottom-end" | ||
trigger="click" | ||
closeOnOutsideClick | ||
tooltip={ | ||
<div className="addon-dropdown"> | ||
{CHANNELS.map(channel => ( | ||
<IconButton | ||
className={'addon-dropdown__item' + (channel === currentChannel ? ' active' : '')} | ||
key={channel} | ||
onClick={() => applyChannel(channel)} | ||
> | ||
{channel} | ||
</IconButton> | ||
))} | ||
</div> | ||
} | ||
> | ||
<IconButton size="medium">Chanel: {currentChannel}</IconButton> | ||
</WithTooltip> | ||
|
||
{/* Mode dropdown */} | ||
<WithTooltip | ||
placement="bottom-end" | ||
trigger="click" | ||
closeOnOutsideClick | ||
tooltip={ | ||
<div className="addon-dropdown"> | ||
{MODES.map(mode => ( | ||
<IconButton | ||
className={'addon-dropdown__item' + (mode === currentMode ? ' active' : '')} | ||
key={mode} | ||
onClick={() => applyMode(mode)} | ||
> | ||
{mode} | ||
</IconButton> | ||
))} | ||
</div> | ||
} | ||
> | ||
<IconButton size="medium">Mode: {currentMode}</IconButton> | ||
</WithTooltip> | ||
</> | ||
); | ||
} | ||
|
||
export default StylesSwitcher; |
16 changes: 16 additions & 0 deletions
16
packages/documentation/.storybook/addons/styles-switcher/register.tsx
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,16 @@ | ||
import React from 'react'; | ||
|
||
import { addons, types } from '@storybook/manager-api'; | ||
import StylesSwitcher from './StylesSwitcher'; | ||
|
||
const ADDON_ID = 'postStylesSwitcher'; | ||
|
||
addons.register(ADDON_ID, () => { | ||
addons.add(ADDON_ID, { | ||
title: 'Switch the documentation styles', | ||
type: types.TOOL, | ||
render: () => { | ||
return <StylesSwitcher />; | ||
}, | ||
}); | ||
}); |
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
34 changes: 30 additions & 4 deletions
34
packages/documentation/.storybook/helpers/open-full-screen-demo.ts
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
Oops, something went wrong.