-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: backgroundPresets add contrast color (#335)
Co-authored-by: faga <lzc295592@163.com> Co-authored-by: Guillaume Chau <guillaume.b.chau@gmail.com>
- Loading branch information
1 parent
086235d
commit c7ced23
Showing
14 changed files
with
167 additions
and
16 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
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,57 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('background color', () => { | ||
const getIframeBody = () => cy.get('iframe[data-test-id="preview-iframe"]') | ||
.its('0.contentDocument.body').should('not.be.empty') | ||
.then(cy.wrap) | ||
|
||
const backgroundColorShouldBe = [ | ||
'rgba(0, 0, 0, 0)', | ||
'rgb(255, 255, 255)', | ||
'rgb(170, 170, 170)', | ||
'rgb(51, 51, 51)', | ||
'rgb(0, 0, 0)', | ||
'rgb(202, 255, 245)', | ||
] | ||
|
||
const contrastColorShouldBe = [ | ||
'rgb(51, 51, 51)', | ||
'rgb(51, 51, 51)', | ||
'rgb(0, 0, 0)', | ||
'rgb(255, 255, 255)', | ||
'rgb(238, 238, 238)', | ||
'rgb(0, 81, 66)', | ||
] | ||
|
||
it('should provide background and contrast color (no iframe)', () => { | ||
cy.visit('/story/src-components-complexparameter-story-vue?variantId=_default') | ||
cy.get('[data-test-id="toolbar-background"]').click() | ||
cy.get('[data-test-id="background-popper"]').should('be.visible').find('button').should('have.length', 6).each(($el, index) => { | ||
cy.wrap($el).click() | ||
cy.get('[data-test-id="responsive-preview-bg"]').should('have.css', 'background-color', backgroundColorShouldBe[index]) | ||
cy.get('[data-test-id="story-variant-single-view"] .native-story').should('have.css', 'color', contrastColorShouldBe[index]) | ||
cy.get('[data-test-id="toolbar-background"]').click() | ||
}) | ||
}) | ||
|
||
it('should provide background and contrast color (with iframe)', () => { | ||
cy.visit('story/src-components-contrastcolor-story-vue?variantId=_default') | ||
cy.get('[data-test-id="toolbar-background"]').click() | ||
cy.get('[data-test-id="background-popper"]').should('be.visible').find('button').should('have.length', 6).each(($el, index) => { | ||
cy.wrap($el).click() | ||
getIframeBody().find('.contrast-color').should('have.css', 'color', contrastColorShouldBe[index]) | ||
cy.get('[data-test-id="toolbar-background"]').click() | ||
}) | ||
}) | ||
|
||
it('should provide background and contrast color (grid)', () => { | ||
cy.visit('/story/src-components-substory-story-vue?variantId=src-components-substory-story-vue-0') | ||
cy.get('[data-test-id="toolbar-background"]').click() | ||
cy.get('[data-test-id="background-popper"]').should('be.visible').find('button').should('have.length', 6).each(($el, index) => { | ||
cy.wrap($el).click() | ||
cy.get('[data-test-id="responsive-preview-bg"]').should('have.css', 'background-color', backgroundColorShouldBe[index]) | ||
cy.get('.__histoire-sandbox .text').should('have.css', 'color', contrastColorShouldBe[index]) | ||
cy.get('[data-test-id="toolbar-background"]').click() | ||
}) | ||
}) | ||
}) |
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,9 +1,17 @@ | ||
import { defineConfig } from 'histoire' | ||
import { defineConfig, getDefaultConfig } from 'histoire' | ||
import { HstVue } from '@histoire/plugin-vue' | ||
|
||
export default defineConfig({ | ||
// outDir: 'hdist', | ||
plugins: [ | ||
HstVue(), | ||
], | ||
backgroundPresets: [ | ||
...(getDefaultConfig().backgroundPresets || []), | ||
{ | ||
label: 'Custom gray', | ||
color: '#cafff5', | ||
contrastColor: '#005142', | ||
}, | ||
], | ||
}) |
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,11 @@ | ||
<template> | ||
<Story> | ||
<span class="contrast-color">Contrast color</span> | ||
</Story> | ||
</template> | ||
|
||
<style scoped> | ||
.contrast-color{ | ||
color: var(--histoire-contrast-color) | ||
} | ||
</style> |
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
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 |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import { reactive } from 'vue' | ||
import { histoireConfig } from './config' | ||
import type { PreviewSettings } from '../types' | ||
|
||
export const receivedSettings = reactive<PreviewSettings>({} as PreviewSettings) | ||
|
||
export function applyPreviewSettings (settings: PreviewSettings) { | ||
Object.assign(receivedSettings, settings) | ||
|
||
// Text direction | ||
document.documentElement.setAttribute('dir', settings.textDirection) | ||
|
||
// Contrast color | ||
document.documentElement.style.setProperty('--histoire-contrast-color', getContrastColor(settings)) | ||
} | ||
|
||
export function getContrastColor (setting: PreviewSettings) { | ||
return histoireConfig.backgroundPresets.find(preset => preset.color === setting.backgroundColor)?.contrastColor ?? 'unset' | ||
} |
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