Created
October 20, 2021 07:06
-
-
Save thecodedrift/f84b450d9bc3e4e834098c4ad2caf6cf to your computer and use it in GitHub Desktop.
Tailwind Typography not-prose working use case
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
// 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) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not-prose
A working example of
not-prose
Implementation
When looping styles, we drop anything that isn't in a
[k, v]
format wherev
is an object. Those are "common" styles, and we want to merge those down into all our prose sub-elements automatically.For items when
v
is an object, we mergev
on top of a reduced object containing our common styles. Currently this is massively unoptimized as a proof of concept. The[k, v]
syntax was simply easier to reason about at the hour this was written.Caveats
The
max-width
needs work. You simply cannot assignmax-width
on some children (likepre
) without getting some weird side effects. The most likely solution is to take elements wheremax-width
is the incorrect behavior and override those to100%
.Headlines and larger font-size elements also will have the max-width problem. This is because it's based on the width of the 0 and not using
rem
. They probably would need to berem
for this to work reliably.