Skip to content

Commit

Permalink
Allow optout of theme & color
Browse files Browse the repository at this point in the history
  • Loading branch information
colepeters committed Mar 21, 2024
1 parent 6f57727 commit 7cea03b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
19 changes: 7 additions & 12 deletions dist/enhance.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

/**
* For more information please see the documentation at : https://github.com/enhance-dev/enhance-styles#readme
* ENHANCE STYLES
* For more information, please see the documentation at : https://github.com/enhance-dev/enhance-styles#readme
*/


/*** Theme Colors ***/
:root {
--accent-h: 208;
Expand All @@ -18,9 +17,6 @@
--error-s: 94.5%;
--error-l: 43.1%;
--error: hsl(var(--error-h), var(--error-s), var(--error-l));



--gray-100: hsl(0, 0%, 90%);
--gray-200: hsl(0, 0%, 80%);
--gray-300: hsl(0, 0%, 70%);
Expand All @@ -30,29 +26,24 @@
--gray-700: hsl(0, 0%, 30%);
--gray-800: hsl(0, 0%, 20%);
--gray-900: hsl(0, 0%, 10%);

--focus-l: 30%;
accent-color: var(--accent, royalblue);
color-scheme: light dark;
}

:is(a, button, input, textarea, summary):focus:not(:focus-visible) {
outline: none;
}

:is(a, button, input, textarea, summary):focus-visible {
outline: max(var(--focus-size, 1px), 1px) solid var(--accent, royalblue);
outline-offset: var(--focus-offset, 0);
box-shadow: 0 0 0 max(var(--focus-size, 3px), 3px) hsl(var(--accent-h), var(--accent-s), calc(var(--accent-l) + var(--focus-l)))
;
}

:is(a, button, input, textarea, summary):not(:focus):not(:placeholder-shown):invalid {
outline: max(var(--focus-size, 1px), 1px) solid var(--error, crimson);
outline-offset: var(--focus-offset, 0);
box-shadow: 0 0 0 3px hsl(var(--error-h), var(--error-s), calc(var(--error-l) + var(--focus-l)));
}

@media (prefers-color-scheme: dark) {
:root {
--accent-h: 208;
Expand All @@ -64,10 +55,14 @@
--focus-l: -30%;
--fore: var(--light);
--back: var(--dark);

}
}

/*** Spot Colors ***/
:root {
null
}



/*** Type Scale ***/
Expand Down
6 changes: 3 additions & 3 deletions doc-header.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function DocHeader() {
return /*css*/`
/**
* For more information please see the documentation at : https://github.com/enhance-dev/enhance-styles#readme
return /*css*/`/**
* ENHANCE STYLES
* For more information, please see the documentation at : https://github.com/enhance-dev/enhance-styles#readme
*/
`
}
23 changes: 23 additions & 0 deletions test/themeColor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import test from 'tape'

import themeColor from '../theme-color.mjs'

const noThemeColor = {
theme: false,
color: false,
}

const noTheme = {
theme: false
}

const noColor = {
color: false
}

test('themeColor', t => {
t.equal(themeColor({ config: noThemeColor }), '', 'should emit an empty string when `config.theme` and `config.color` are both `false`')
t.notOk(themeColor({ config: noTheme }).includes('/*** Theme Colors ***/'), 'should not includes theme colors when `config.theme` is `false`')
t.notOk(themeColor({ config: noColor }).includes('/*** Spot Colors ***/'), 'should not includes theme colors when `config.color` is `false`')
t.end()
})
37 changes: 25 additions & 12 deletions theme-color.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import hextohsl from './hex-to-hsl.mjs'

export default function themeColor ({ config }) {
export default function themeColor({ config }) {
const { color = {}, theme = {} } = config

if (color === false && theme === false) {
return ''
}

const defaultAccent = '#0075db'
const defaultError = '#d60606'
const defaultLight = '#fefefe'
Expand Down Expand Up @@ -39,20 +43,20 @@ export default function themeColor ({ config }) {

const themeColors = Object.keys(theme).map(name => {
if (name === 'accent' ||
name === 'error' ||
name === 'back' ||
name === 'fore' ||
(name === 'dark' && typeof theme[name] === 'object')) {
name === 'error' ||
name === 'back' ||
name === 'fore' ||
(name === 'dark' && typeof theme[name] === 'object')) {
return
}
else {
return colorSteps(hextohsl(theme[name]), name)
}
}).join('\n')
const colors = Object.keys(color).map(name => ` --${name}: ${color[name]};`).join('\n')
const colors = Object.keys(color).length ? Object.keys(color).map(name => `--${name}: ${color[name]};`).join('\n ') : null
const grayScale = colorSteps({ h: lightParts.h, s: 0, l: 50 }, 'gray')

function colorSteps (color, name) {
function colorSteps(color, name) {
const hue = color.h
const saturation = color.s
const luminance = color.l
Expand All @@ -66,12 +70,10 @@ export default function themeColor ({ config }) {
--${name}-600: hsl(${hue}, ${saturation}%, ${Math.floor(luminance - 10)}%);
--${name}-700: hsl(${hue}, ${saturation}%, ${Math.floor(luminance - 20)}%);
--${name}-800: hsl(${hue}, ${saturation}%, ${Math.floor(luminance - 30)}%);
--${name}-900: hsl(${hue}, ${saturation}%, ${Math.floor(luminance - 40)}%);
`
--${name}-900: hsl(${hue}, ${saturation}%, ${Math.floor(luminance - 40)}%);`
}


return /*css*/`
const themeStyles = `
/*** Theme Colors ***/
:root {
--accent-h: ${lightAccentParts.h};
Expand All @@ -87,7 +89,6 @@ export default function themeColor ({ config }) {
--error-l: ${lightErrorParts.l}%;
--error: hsl(var(--error-h), var(--error-s), var(--error-l));
${themeColors}
${colors}
${grayScale}
--focus-l: 30%;
accent-color: var(--accent, royalblue);
Expand Down Expand Up @@ -125,7 +126,19 @@ ${grayScale}
${darkThemeColors}
}
}
`.replace(/^\s*\n/gm, '') // remove empty newlines

const colorStyles = `
/*** Spot Colors ***/
:root {
${colors}
}
`

let result = ``
if (theme !== false) result += themeStyles
if (color !== false) result += colorStyles

return result
}

0 comments on commit 7cea03b

Please sign in to comment.