Skip to content

Instantly share code, notes, and snippets.

@thecodedrift
Created October 20, 2021 07:06
Show Gist options
  • Select an option

  • Save thecodedrift/f84b450d9bc3e4e834098c4ad2caf6cf to your computer and use it in GitHub Desktop.

Select an option

Save thecodedrift/f84b450d9bc3e4e834098c4ad2caf6cf to your computer and use it in GitHub Desktop.

Revisions

  1. thecodedrift created this gist Oct 20, 2021.
    41 changes: 41 additions & 0 deletions typography-plugin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // just the changed bits
    // and definitely not elegant yet

    // withCommon: extract the top level CSS properties and magically push them down
    // this is probably something you'd want to cache
    function withCommon(v, list) {
    const common = list.filter(([k, v]) => !(typeof v == 'object' && v.constructor == Object))
    return {
    ...common.reduce((merged, [k, vPrime]) => {
    merged[k] = vPrime
    return merged
    }, {}),
    ...v,
    }
    }

    function configToCss(config = {}, { target, className, prefix }) {
    return Object.fromEntries(
    Object.entries(
    merge(
    {},
    ...Object.keys(config)
    .filter((key) => computed[key])
    .map((key) => computed[key](config[key])),
    ...castArray(config.css || {})
    )
    )
    .map(([k, v], idx, list) => {
    if (target === 'legacy') {
    return [k, v]
    }

    if (typeof v == 'object' && v.constructor == Object) {
    return [inWhere(k, { className, prefix }), withCommon(v, list)]
    } else {
    return null // drop all "common" props with our filter
    }
    })
    .filter((v) => !!v)
    )
    }