Skip to content

Commit

Permalink
feat: theme.darkClass and deprecate sandboxDarkClass, fix #442
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 10, 2023
1 parent fd72c48 commit 6ea5bbe
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.
13 changes: 1 addition & 12 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Properties:
- `favicon: string`: Href to the favicon file (**not** processed by Vite). Put the file in the `public` directory.
- `colors: Object`: Customize the colors. Each color should be an object with shades as keys.
- `logoHref: string`: Add a link to the main logo
- `darkClass: string`: Class added to the story preview when dark mode is enabled (default is `'dark'`).
- `defaultColorScheme: 'light' | 'dark' | 'auto'`: Default color scheme for the app. `'auto'` will use the system preference.
- `hideColorSchemeSwitch: boolean`: Hides the dark mode button in the toolbar.
- `storeColorScheme: boolean`: Enable persistence of the color scheme in the browser's local storage.
Expand Down Expand Up @@ -314,18 +315,6 @@ export default defineConfig({
})
```

## `sandboxDarkClass`

`string` - Default: `'dark'`

Class added to the html root of the story preview when dark mode is enabled.

```ts
export default defineConfig({
sandboxDarkClass: 'my-dark-class',
})
```

## `markdown`

`(md: MarkdownIt) => MarkdownIt | Promise<MarkdownIt>`
Expand Down
3 changes: 3 additions & 0 deletions examples/vue3/histoire.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ export default defineConfig({
],
// autoApplyContrastColor: true,
// routerMode: 'hash',
theme: {
darkClass: 'my-dark',
},
})
4 changes: 4 additions & 0 deletions examples/vue3/src/components/BaseButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ defineProps({
font-size: 12px;
padding: 2px 4px;
}
.my-dark .btn {
color: black;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useScrollOnActive } from '../../util/scroll'
import { usePreviewSettingsStore } from '../../stores/preview-settings'
import { getContrastColor } from '../../util/preview-settings'
import { histoireConfig } from '../../util/config'
import { isDark } from '../../util/dark'
import GenericRenderStory from './GenericRenderStory.vue'
import ToolbarNewTab from '../toolbar/ToolbarNewTab.vue'
import CheckerboardPattern from '../misc/CheckerboardPattern.vue'
Expand Down Expand Up @@ -136,6 +137,9 @@ const autoApplyContrastColor = computed(() => !!histoireConfig.autoApplyContrast
:variant="variant"
:story="story"
:dir="settings.textDirection"
:class="{
[histoireConfig.theme.darkClass]: isDark,
}"
@ready="onReady"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const autoApplyContrastColor = computed(() => !!histoireConfig.autoApplyContrast
:variant="variant"
:story="story"
class="htw-h-full"
:class="[ isDark ? histoireConfig.sandboxDarkClass : undefined ]"
:class="{
[histoireConfig.sandboxDarkClass]: isDark, // @TODO remove
[histoireConfig.theme.darkClass]: isDark,
}"
:dir="settings.textDirection"
@ready="onReady"
/>
Expand Down
6 changes: 4 additions & 2 deletions packages/histoire-app/src/app/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ app.mount('#app')

watch(isDark, value => {
if (value) {
document.documentElement.classList.add(histoireConfig.sandboxDarkClass)
document.documentElement.classList.add(histoireConfig.sandboxDarkClass) // @TODO remove
document.documentElement.classList.add(histoireConfig.theme.darkClass)
} else {
document.documentElement.classList.remove(histoireConfig.sandboxDarkClass)
document.documentElement.classList.remove(histoireConfig.sandboxDarkClass) // @TODO remove
document.documentElement.classList.remove(histoireConfig.theme.darkClass)
}
}, {
immediate: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/histoire-shared/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export interface HistoireConfig {
* Enable persistence of the color scheme in the browser.
*/
storeColorScheme?: boolean
/**
* Class added to the story preview when dark mode is enabled.
*/
darkClass?: string
}
/**
* Setup file exporting a default function executed when setting up each story preview.
Expand Down Expand Up @@ -174,6 +178,7 @@ export interface HistoireConfig {
autoApplyContrastColor?: boolean
/**
* Class added to the html root of the story preview when dark mode is enabled.
* @deprecated use `theme.darkClass` instead
*/
sandboxDarkClass?: string
/**
Expand Down
1 change: 1 addition & 0 deletions packages/histoire/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function getDefaultConfig (): HistoireConfig {
},
defaultColorScheme: 'auto',
storeColorScheme: true,
darkClass: 'dark',
},
responsivePresets: [
{
Expand Down

0 comments on commit 6ea5bbe

Please sign in to comment.