Skip to content
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

feat: backgroundPresets add contrast color #335

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/reference/config.md
Expand Up @@ -252,6 +252,7 @@ Each object in the array is a preset with the following properties:

- `label: string`: Label for the preset.
- `color: string`: Color of the preset.
- `contrastColor?: string`: Contrast color of preset

Default values are shown in the example below:

Expand All @@ -261,26 +262,32 @@ export default defineConfig({
{
label: 'Transparent',
color: 'transparent',
contrastColor: '#333'
},
{
label: 'White',
color: '#fff',
contrastColor: '#333'
},
{
label: 'Light gray',
color: '#aaa',
contrastColor: '#eee'
},
{
label: 'Dark gray',
color: '#333',
contrastColor: '#ccc'
},
{
label: 'Black',
color: '#000',
contrastColor: '#fff'
},
],
})
```
You can use current contrast color via the css variable `--contrast-color`.

## `sandboxDarkClass`

Expand Down
6 changes: 6 additions & 0 deletions packages/histoire-app/src/app/App.vue
Expand Up @@ -22,6 +22,8 @@ import Breadcrumb from './components/app/Breadcrumb.vue'
import SearchModal from './components/search/SearchModal.vue'
import InitialLoading from './components/app/InitialLoading.vue'
import GenericMountStory from './components/story/GenericMountStory.vue'
import { usePreviewSettingsStore } from './stores/preview-settings'
import { setContrastColor } from './util/contrastColor'

const files = ref<StoryFile[]>(rawFiles.map(file => mapFile(file)))
const tree = ref<Tree>(rawTree)
Expand All @@ -40,6 +42,10 @@ const stories = computed(() => files.value.reduce((acc, file) => {
return acc
}, []))

// contrast color
const settings = usePreviewSettingsStore().currentSettings
watch(settings, () => setContrastColor(settings), { immediate: true })
Akryum marked this conversation as resolved.
Show resolved Hide resolved

// Store

const storyStore = useStoryStore()
Expand Down
7 changes: 7 additions & 0 deletions packages/histoire-app/src/app/util/contrastColor.ts
@@ -0,0 +1,7 @@
import { histoireConfig } from './config'
import type { PreviewSettings } from '../types'

export function setContrastColor (setting: PreviewSettings) {
const contrastColor = histoireConfig.backgroundPresets.find(preset => preset.color === setting.backgroundColor)?.contrastColor
document.documentElement.style.setProperty('--contrast-color', contrastColor)
}
1 change: 1 addition & 0 deletions packages/histoire-shared/src/types/config.ts
Expand Up @@ -25,6 +25,7 @@ export interface ResponsivePreset {
export interface BackgroundPreset {
label: string
color: string
contrastColor?: string
}

export interface TreeGroupConfig {
Expand Down
5 changes: 5 additions & 0 deletions packages/histoire/src/node/config.ts
Expand Up @@ -93,22 +93,27 @@ export function getDefaultConfig (): HistoireConfig {
{
label: 'Transparent',
color: 'transparent',
contrastColor: '#333',
},
{
label: 'White',
color: '#fff',
contrastColor: '#333',
},
{
label: 'Light gray',
color: '#aaa',
contrastColor: '#eee',
},
{
label: 'Dark gray',
color: '#333',
contrastColor: '#ccc',
},
{
label: 'Black',
color: '#000',
contrastColor: '#fff',
},
],
sandboxDarkClass: 'dark',
Expand Down