Skip to content

Commit

Permalink
let you style classes
Browse files Browse the repository at this point in the history
  • Loading branch information
CarelessCourage committed Jan 3, 2024
1 parent b059e55 commit f5b0a09
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/core/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getReadable } from './primitives/color'
import { umbraGenerate } from './generator'

interface ApplyProps {
element?: HTMLElement
element?: HTMLElement | string
formater?: Formater
alias?: Alias | boolean
}
Expand Down
31 changes: 18 additions & 13 deletions packages/core/engine/primitives/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ const defaultAliases = {
interface Attach {
input: UmbraInput
outputs: UmbraOutputs
element?: HTMLElement | null
element?: string | HTMLElement | null
alias?: Alias | boolean
}

//main
export function attach({ input, outputs, element, alias }: Attach) {
setColors(outputs.flattened, element)
setAliases(alias || input.settings.aliases, element)
//setAliases(alias || input.settings.aliases, element)

if (!element) return outputs
if (typeof element === 'string') return outputs
//Ensure that the foreground color is always set to the attached element
setProperty(element, {
name: 'color',
Expand Down Expand Up @@ -114,46 +115,50 @@ function invalidColor(name: string) {
}

//attach colors
function setColors(flattened: FlattenColor[], element?: HTMLElement | null) {
function setColors(flattened: FlattenColor[], element?: string | HTMLElement | null) {
const filtered = flattened.filter(({ name }) => !invalidColor(name))
element ? setElementColors(element, filtered) : setColorSheet(filtered)
const notHTMLElement = typeof element !== 'object'
notHTMLElement ? setColorSheet(element, filtered) : setElementColors(element, filtered)
}

function setColorSheet(flattened: FlattenColor[]) {
function setColorSheet(element = ':root', flattened: FlattenColor[]) {
const sheet = new CSSStyleSheet()
sheet.replace(`:root {${flattened.map(({ name, color }) => `${name}: ${color};`).join('')}}`)
sheet.replace(`${element} {${flattened.map(({ name, color }) => `${name}: ${color};`).join('')}}`)
document.adoptedStyleSheets = [sheet]
}

function setElementColors(element: HTMLElement, colors: FlattenColor[]) {
function setElementColors(element: HTMLElement | null, colors: FlattenColor[]) {
if (!element) return
colors.forEach(({ name, color }) => {
setProperty(element, { name, color })
})
}

//attach aliases
function setAliases(aliases?: Alias | true, element?: HTMLElement | null) {
function setAliases(aliases?: Alias | true, element?: string | HTMLElement | null) {
if (!aliases) return
const ali = aliases === true ? defaultAliases : aliases
const aliasesArray = makeAliasArray(ali)
element ? setElementAliases(element, aliasesArray) : setAliasesSheet(aliasesArray)
const notHTMLElement = typeof element !== 'object'
notHTMLElement ? setAliasesSheet(element, aliasesArray) : setElementAliases(element, aliasesArray)
}

function setAliasesSheet(aliases: AliasObject[]) {
function setAliasesSheet(element = ':root', aliases: AliasObject[]) {
function camelToVariable(name: string) {
return '--' + name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}

const sheet = new CSSStyleSheet()
sheet.replace(
`:root {${aliases
`:${element} {${aliases
.map(({ name, value }) => `${camelToVariable(name)}: var(--${value});`)
.join('')}}`
)
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
document.adoptedStyleSheets = [sheet]
}

function setElementAliases(element: HTMLElement, aliases: AliasObject[]) {
function setElementAliases(element: HTMLElement | null, aliases: AliasObject[]) {
if (!element) return
aliases.forEach(({ name, value }) => {
setProperty(element, {
name: name,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/engine/primitives/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const format = ({

return {
...outputs,
attach: (element?: HTMLElement, alias?: Alias | boolean) => {
attach: (element?: HTMLElement | string, alias?: Alias | boolean) => {
if (!document) return outputs
return attach({ input, outputs, element, alias })
}
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const theme = umbra({
background: '#000000',
foreground: '#ffffff',
accents: [radixBlue, radixRed, radixYellow, success, royal, brown, something, accent]
}).apply({ alias: true })
}).apply({ element: 'html' })
const t = ref(theme.input)
const formated = ref(theme.formated)
Expand Down

0 comments on commit f5b0a09

Please sign in to comment.