forked from shoelace-style/shoelace
-
Notifications
You must be signed in to change notification settings - Fork 0
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
External icon libraries & token generation #18
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
416b19e
Exporting a method to automatically register Font Awesome
CrookedGrin 76b263d
Token generation from tailwind theme
CrookedGrin 571f350
Update code-block.js
CrookedGrin dc576f0
Replacing JS with JSON
CrookedGrin d54f5d7
Update .prettierignore
CrookedGrin cb24e65
Update external.ts
CrookedGrin cd74335
Prettier
CrookedGrin ed8effb
Removing comments
CrookedGrin 1cb4f3e
Trying to get CI to not error out
CrookedGrin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ src/react/index.ts | |
node_modules | ||
package-lock.json | ||
tsconfig.json | ||
src/styles/exports/generated.css | ||
|
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
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,111 @@ | ||
/* Read from the JSON source of truth and write a css file using the template */ | ||
|
||
import chalk from 'chalk'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import prettier from 'prettier'; | ||
import tokens from './../src/styles/tokens.json' assert { type: 'json' }; | ||
|
||
const colorFamilies = ['blue', 'gray', 'red', 'green', 'yellow', 'teal', 'purple', 'fuchsia']; | ||
const colors = tokens.colors; | ||
const variablePrefix = '--ts-color-'; | ||
|
||
function hexToRgb(hex) { | ||
const hexValue = hex.replace('#', ''); | ||
const r = parseInt(hexValue.substring(0, 2), 16); | ||
const g = parseInt(hexValue.substring(2, 4), 16); | ||
const b = parseInt(hexValue.substring(4, 6), 16); | ||
return { r, g, b }; | ||
} | ||
|
||
function generateColorVariables(colorFamily) { | ||
let css = ` /** ${colorFamily} */\n`; | ||
let hexCss = ``; | ||
let rgbCss = ``; | ||
for (const weight in colors[colorFamily]) { | ||
const colorValue = colors[colorFamily][weight]; | ||
hexCss += ` ${variablePrefix}${colorFamily}-${weight}: ${colorValue};\n`; | ||
const rgbValue = hexToRgb(colorValue); | ||
rgbCss += ` ${variablePrefix}${colorFamily}-${weight}-rgb: ${rgbValue.r}, ${rgbValue.g}, ${rgbValue.b};\n`; | ||
} | ||
css += hexCss + rgbCss; | ||
return css; | ||
} | ||
|
||
function generateSemanticVariants(semanticFamily, colorFamily) { | ||
let css = ` /** ${semanticFamily} => ${colorFamily} */\n`; | ||
for (const weight in colors[colorFamily]) { | ||
css += ` ${variablePrefix}${semanticFamily}-${weight}: var(${variablePrefix}${colorFamily}-${weight});\n`; | ||
} | ||
return css; | ||
} | ||
|
||
function generateShoelaceColorOverride(colorFamily) { | ||
let css = ` /** ${colorFamily} override */\n`; | ||
// Use the weights from blue for all of these | ||
for (const weight in colors['blue']) { | ||
css += ` --sl-color-${colorFamily}-${weight}: var(${variablePrefix}${colorFamily}-${weight});\n`; | ||
} | ||
return css; | ||
} | ||
|
||
console.log('Generating tokens CSS'); | ||
|
||
let cssContent = | ||
`/** *********************************************************************\n` + | ||
`/** Tokens generated from /src/styles/tokens.json\n` + | ||
`/** Do not edit this file directly. Make changes in the json\n` + | ||
`/** and then run 'npm run build'\n` + | ||
`/** ********************************************************************* **/\n` + | ||
`\n` + | ||
`:host,\n:root,\n.sl-theme-light {\n` + | ||
`\n\n /***** Generated colors **/\n\n`; | ||
|
||
for (const colorFamily of colorFamilies) { | ||
if (colors[colorFamily]) { | ||
cssContent += generateColorVariables(colorFamily); | ||
} | ||
} | ||
|
||
cssContent += `\n /***** Semantic color variants **/\n\n`; | ||
|
||
const semanticHash = { | ||
primary: 'blue', | ||
success: 'green', | ||
warning: 'yellow', | ||
danger: 'red', | ||
neutral: 'gray' | ||
}; | ||
for (const semanticFamily in semanticHash) { | ||
cssContent += generateSemanticVariants(semanticFamily, semanticHash[semanticFamily]); | ||
} | ||
|
||
cssContent += `\n /***** Shoelace color overrides **/\n\n`; | ||
|
||
const shoelaceColors = colorFamilies.concat(Object.keys(semanticHash)); | ||
for (const shoelaceColor of shoelaceColors) { | ||
cssContent += generateShoelaceColorOverride(shoelaceColor); | ||
} | ||
|
||
cssContent += `\n /***** Other generated tokens **/\n\n`; | ||
|
||
const font = tokens.fontFamily; | ||
cssContent += | ||
` /**** Generated fonts **/\n` + | ||
` --ts-font-sans: ${font.sans};\n` + | ||
` --ts-font-serif: ${font.serif};\n` + | ||
` --ts-font-mono: ${font.mono};\n` + | ||
` --ts-font-body: var(--ts-font-sans);\n` + | ||
` --ts-font-display: var(--ts-font-serif);\n`; | ||
|
||
cssContent += `}\n`; | ||
|
||
cssContent = prettier.format(cssContent, { parser: 'css' }); | ||
|
||
// Write the CSS file | ||
try { | ||
const fileName = path.join('./src/styles/exports', 'generated.css'); | ||
fs.writeFileSync(fileName, cssContent, 'utf8'); | ||
} catch (err) { | ||
console.error(chalk.red('Error writing CSS file:'), err); | ||
} |
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,35 @@ | ||
import { registerIconLibrary } from './library'; | ||
|
||
/* Teamshares-specific icon libraries */ | ||
export function registerExternalLibraries() { | ||
registerIconLibrary('fa-free', { | ||
resolver: name => { | ||
const filename = name.replace(/^fa[rbs]-/, ''); | ||
let folder = 'regular'; | ||
if (name.startsWith('fas-')) folder = 'solid'; | ||
if (name.startsWith('fab-')) folder = 'brands'; | ||
return `https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.3.0/svgs/${folder}/${filename}.svg`; | ||
}, | ||
mutator: svg => svg.setAttribute('fill', 'currentColor') | ||
}); | ||
|
||
registerIconLibrary('fa', { | ||
resolver: name => { | ||
const filename = name.replace(/^fa([rsltdb]|(ss))-/, ''); | ||
const sub = name.substring(0, 4); | ||
const folderHash = { | ||
'fas-': 'solid', | ||
'fal-': 'light', | ||
'fat-': 'thin', | ||
'fad-': 'duotone', | ||
'fab-': 'brands' | ||
}; | ||
const folder: unknown = folderHash[sub as keyof typeof folderHash] || 'regular'; | ||
/* Note: The token refers to the Teamshares Font Awesome Kit */ | ||
/* See https://fontawesome.com/kits/44da2a9d09/setup */ | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
return `https://ka-p.fontawesome.com/releases/v6.4.0/svgs/${folder}/${filename}.svg?token=44da2a9d09`; | ||
}, | ||
mutator: svg => svg.setAttribute('fill', 'currentColor') | ||
}); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL
import
+ assert`There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a new / experimental Node thing. You get a warning in the console, but it works.