Skip to content

Commit

Permalink
feat(theme): define custom properties for all font styles and curves (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
daenub authored Feb 29, 2024
1 parent a039032 commit fadafde
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 30 deletions.
2 changes: 2 additions & 0 deletions postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const postcssPresetEnv = require("postcss-preset-env")
const atImport = require("postcss-import")
const leuFontStyles = require("./scripts/postcss-leu-font-styles.cjs")

module.exports = {
stage: 2,
plugins: [
atImport(),
leuFontStyles(),
postcssPresetEnv({
features: {
"custom-media-queries": true,
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-component/templates/[name].css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
}

:host {
--[name]-font-regular: var(--leu-font-regular);
--[name]-font-black: var(--leu-font-black);
--[name]-font-regular: var(--leu-font-family-regular);
--[name]-font-black: var(--leu-font-family-black);

font-family: var(--[name]-font-regular);
}
160 changes: 160 additions & 0 deletions scripts/postcss-leu-font-styles.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
const path = require("path")
const fs = require("fs/promises")

/* Plugin logic */

function getPixelValue(value) {
return parseInt(value.replace("px", ""), 10)
}

async function parseFile(file) {
const string = await fs.readFile(file, "utf-8")

return JSON.parse(string)
}

function generateCustomPropertyDeclarations({
identifier,
fontSize,
fontWeight,
lineHeight,
spacing,
}) {
const customPropertyPrefix = `--leu-t-${identifier}-${fontWeight}`

const varFontSize = `${customPropertyPrefix}-font-size`
const varLineHeight = `${customPropertyPrefix}-line-height`
const varSpacing = `${customPropertyPrefix}-spacing`
const varFont = `${customPropertyPrefix}-font`
const varFontFamily = `--leu-font-family-${fontWeight}`

return [
{ type: "fontSize", name: varFontSize, value: `${fontSize / 16}rem` },
{ type: "lineHeight", name: varLineHeight, value: `${lineHeight}` },
{ type: "spacing", name: varSpacing, value: `${spacing}rem` },
{
type: "font",
name: varFont,
value: `var(${varFontSize}) / var(${varLineHeight}) var(${varFontFamily})`,
},
]
}

function curveStepDeclarations(curvePrefix, stepStyle) {
const fontSizeVar = stepStyle.declarations.find(
(s) => s.type === "fontSize"
).name
const lineHeightVar = stepStyle.declarations.find(
(s) => s.type === "lineHeight"
).name
const spacingVar = stepStyle.declarations.find(
(s) => s.type === "spacing"
).name
const fontVar = stepStyle.declarations.find((s) => s.type === "font").name

return [
{ prop: `${curvePrefix}-font-size`, value: ` var(${fontSizeVar})` },
{ prop: `${curvePrefix}-line-height`, value: ` var(${lineHeightVar})` },
{ prop: `${curvePrefix}-spacing`, value: ` var(${spacingVar})` },
{ prop: `${curvePrefix}-font`, value: ` var(${fontVar})` },
]
}

/**
*
* @param {*} file
* @param {import('postcss')} postcss
* @param {import('postcss').Source} nodeSource
* @returns
*/
async function createLeuFontStyleNodes(file, postcss, nodeSource) {
const { definitions, curves } = await parseFile(file)

const fontStyleDeclarations = definitions.flatMap((style) => {
const fontSize = getPixelValue(style.fontSize)
const fontWeights = Array.isArray(style.fontWeight)
? style.fontWeight
: [style.fontWeight]
const spacing = getPixelValue(style.spacing) / 16

const identifier = (fontSize / 4) * 10

return fontWeights.map((fontWeight) => ({
name: style.name,
fontWeight,
identifier,
declarations: generateCustomPropertyDeclarations({
identifier,
fontSize,
fontWeight,
lineHeight: style.lineHeight,
spacing,
}),
}))
})

const fontStyleNodes = fontStyleDeclarations.flatMap((style) =>
style.declarations.map(
({ name, value }) =>
new postcss.Declaration({ prop: name, value, source: nodeSource })
)
)

const curveNodes = curves.flatMap((curve) => {
const [_, lastStepName] = curve.steps.at(-1)
const { identifier } = fontStyleDeclarations.find(
(style) => style.name === lastStepName && style.fontWeight === "black"
)

const curvePrefix = `--leu-t-curve-${identifier}-black`

return curve.steps.flatMap((step) => {
const [viewport, styleName] = step

const stepStyle = fontStyleDeclarations.find(
(s) => s.name === styleName && s.fontWeight === "black"
)

const nodes = curveStepDeclarations(curvePrefix, stepStyle).map(
({ prop, value }) =>
new postcss.Declaration({ prop, value, source: nodeSource })
)

return viewport === null
? nodes
: new postcss.AtRule({
name: "media",
params: `(${viewport})`,
nodes,
source: nodeSource,
})
})
})

return [...fontStyleNodes, ...curveNodes]
}

/* Plugin config */

/**
* @type {import('postcss').PluginCreator}
*/
module.exports = () => ({
postcssPlugin: "leu-font-styles",
AtRule: {
"leu-font-styles": async (atRule, postcss) => {
const rootDir = path.dirname(atRule.source.input.file)
const jsonFile = atRule.params.replace(/['"]+/g, "")

const nodes = await createLeuFontStyleNodes(
path.resolve(rootDir, jsonFile),
postcss,
atRule.source
)

atRule.replaceWith(nodes)
},
},
})

module.exports.postcss = true
4 changes: 2 additions & 2 deletions src/components/accordion/accordion.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
}

:host {
--accordion-font-regular: var(--leu-font-regular);
--accordion-font-black: var(--leu-font-black);
--accordion-font-regular: var(--leu-font-family-regular);
--accordion-font-black: var(--leu-font-family-black);

--accordion-toggle-font: var(--accordion-font-black);

Expand Down
4 changes: 2 additions & 2 deletions src/components/breadcrumb/breadcrumb.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
}

:host {
--breadcrumb-font-regular: var(--leu-font-regular);
--breadcrumb-font-black: var(--leu-font-black);
--breadcrumb-font-regular: var(--leu-font-family-regular);
--breadcrumb-font-black: var(--leu-font-family-black);

font-family: var(--breadcrumb-font-regular);
line-height: 1.5;
Expand Down
4 changes: 2 additions & 2 deletions src/components/button/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
}

button {
font-family: var(--leu-font-black);
font-family: var(--leu-font-family-black);
text-align: center;
appearance: none;
transition: background 0.1s ease;
Expand Down Expand Up @@ -107,7 +107,7 @@ button.ghost {
background: transparent;
padding: 0 0.5rem;
color: var(--leu-color-black-60);
font-family: var(--leu-font-regular);
font-family: var(--leu-font-family-regular);
}

button.ghost:hover {
Expand Down
4 changes: 2 additions & 2 deletions src/components/checkbox/checkbox-group.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
:host {
--group-font-regular: var(--leu-font-regular);
--group-font-black: var(--leu-font-black);
--group-font-regular: var(--leu-font-family-regular);
--group-font-black: var(--leu-font-family-black);

font-family: var(--group-font-regular);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/checkbox/checkbox.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

--checkbox-tick-color: var(--leu-color-black-0);

--checkbox-font-regular: var(--leu-font-regular);
--checkbox-font-regular: var(--leu-font-family-regular);

position: relative;

Expand Down
4 changes: 2 additions & 2 deletions src/components/chip/chip.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
--chip-radio-background-default: var(--leu-color-black-0);
--chip-radio-background-selected: var(--leu-color-func-cyan);

--chip-font-regular: var(--leu-font-regular);
--chip-font-black: var(--leu-font-black);
--chip-font-regular: var(--leu-font-family-regular);
--chip-font-black: var(--leu-font-family-black);

--chip-background-color: var(--chip-background-color-default);
--chip-color: var(--chip-color-default);
Expand Down
4 changes: 2 additions & 2 deletions src/components/input/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

--input-clear-color: var(--leu-color-black-60);

--input-font-regular: var(--leu-font-regular);
--input-font-black: var(--leu-font-black);
--input-font-regular: var(--leu-font-family-regular);
--input-font-black: var(--leu-font-family-black);

display: block;
font-family: var(--input-font-regular);
Expand Down
6 changes: 3 additions & 3 deletions src/components/menu/menu-item.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
--background-disabled: var(--leu-color-black-black-0);
--color: var(--leu-color-black-transp-60);
--color-disabled: var(--leu-color-black-transp-20);
--font-regular: var(--leu-font-regular);
--font-black: var(--leu-font-black);
--font-regular: var(--leu-font-family-regular);
--font-black: var(--leu-font-family-black);

font-family: var(--leu-font-regular);
font-family: var(--leu-font-family-regular);
}

.button {
Expand Down
2 changes: 1 addition & 1 deletion src/components/pagination/pagination.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
margin-top: 16px;
display: flex;
justify-content: end;
font-family: var(--leu-font-regular);
font-family: var(--leu-font-family-regular);
}

.input {
Expand Down
4 changes: 2 additions & 2 deletions src/components/popup/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
}

:host {
--popup-font-regular: var(--leu-font-regular);
--popup-font-black: var(--leu-font-black);
--popup-font-regular: var(--leu-font-family-regular);
--popup-font-black: var(--leu-font-family-black);

font-family: var(--popup-font-regular);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/radio/radio-group.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
:host {
--group-font-regular: var(--leu-font-regular);
--group-font-black: var(--leu-font-black);
--group-font-regular: var(--leu-font-family-regular);
--group-font-black: var(--leu-font-family-black);

font-family: var(--group-font-regular);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/radio/radio.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
--radio-label-color: var(--leu-color-black-100);
--radio-label-color-disabled: var(--radio-color-disabled);

--radio-font-regular: var(--leu-font-regular);
--radio-font-regular: var(--leu-font-family-regular);

display: inline-flex;
align-items: flex-start;
Expand Down
4 changes: 2 additions & 2 deletions src/components/select/select.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

--select-clear-color: var(--leu-color-black-60);

--select-font-regular: var(--leu-font-regular);
--select-font-black: var(--leu-font-black);
--select-font-regular: var(--leu-font-family-regular);
--select-font-black: var(--leu-font-family-black);

--select-apply-button-color: var(--leu-color-black-100);
--select-apply-button-color-focus: var(--leu-color-black-80);
Expand Down
4 changes: 2 additions & 2 deletions src/components/table/table.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ table {
border-spacing: 0;
color: rgb(0 0 0 / 60%);
font-size: 16px;
font-family: var(--leu-font-regular);
font-family: var(--leu-font-family-regular);
line-height: 1.5;
}

Expand All @@ -41,7 +41,7 @@ th {
text-align: left;
font-size: 12px;
font-weight: normal;
font-family: var(--leu-font-black);
font-family: var(--leu-font-family-black);
background: var(--table-even-row-bg);
}

Expand Down
8 changes: 6 additions & 2 deletions src/styles/custom-properties.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url("./custom-media.css");

:root {
--leu-color-black-100: #000;
--leu-color-black-80: #333;
Expand Down Expand Up @@ -42,10 +44,12 @@
--leu-color-func-red: #d93c1a;
--leu-color-func-green: #1a7f1f;

--leu-font-regular: HelveticaNowRegular, Helvetica, sans-serif; /* stylelint-disable-line value-keyword-case */
--leu-font-black: HelveticaNowBlack, Arial Black, Helvetica, sans-serif; /* stylelint-disable-line value-keyword-case */
--leu-font-family-regular: HelveticaNowRegular, Helvetica, sans-serif; /* stylelint-disable-line value-keyword-case */
--leu-font-family-black: HelveticaNowBlack, Arial Black, Helvetica, sans-serif; /* stylelint-disable-line value-keyword-case */

--leu-box-shadow-short: 0px 0px 2px var(--leu-color-black-transp-40);
--leu-box-shadow-regular: 0px 0px 16px var(--leu-color-black-transp-20);
--leu-box-shadow-long: 0px 0px 80px var(--leu-color-black-transp-20);

@leu-font-styles './font-definitions.json';
}
Loading

0 comments on commit fadafde

Please sign in to comment.