Skip to content

Instantly share code, notes, and snippets.

Avatar
😍
Writing CSS

Tommy Hodgins tomhodgins

😍
Writing CSS
View GitHub Profile
View wait-for-react.js
// Wait for React
function hasReactProperty(tag = document.documentElement) {
return Object.keys(tag).some(key =>
key.includes('__react')
)
}
function isReactRoot(tag) {
return hasReactProperty(tag)
View two-modules-zero-files.html
<script type=module src="data:text/javascript,
import {demo} from 'data:text/javascript,export const demo = `Howdy`';
console.log(demo);
"></script>
View gist:b4f922049cace16e7401aebac0b45848
Sass (and Less and Stylus and other custom languages that compile to CSS) have some design flaws that end up limiting what you can do with CSS. If you have a codebase that still depends on them it would be a good time to isolate and minimize the code you have that uses them and strategize how you'll migrate away from them. If your project doesn't have have them, or you haven't yet learned them - it might be a good idea, thinking ahead, to just skip that and get into a pure CSS workflow. Don't worry, you can still preprocess CSS, the secret is writing and handling valid CSS at every step in your workflow so all the tools work together
Some flaws Sass has that get in the way:
- It's not actually compatible with CSS syntax. It's not a super-set, it's an entirely different syntax and because it's incompatible with real CSS, there's an infinite variety of things you might have or want in CSS that can't even be present in any stylesheet Sass looks at
- Some of the things Sass adds to its custom syntax are things
View id-overview.js
function html(strings, ...expressions) {
return document.createRange().createContextualFragment(
strings.reduce((output, string, index) =>
output + expressions[index - 1] + string
)
)
}
document.body.append(
html`
View dom-toolkit.md
View how-to-extend-a-language.md

Ways to extend a language with custom stuff that works™

  • define stuff in the language to use (libraries)
  • if the language lets you define custom parts of syntax (macros, plugins, etc) use the built-in methods for extending the language itself
  • create an entirely separate DSL for working with solutions in this problem space and figure out how that maps to this language

Ways to extend a language with custom stuff that doesn't work™

  • try to add additional syntax as a thin layer on top of a separate, growing language still under development
View extract-custom-properties.js
const extractCustomProperties = (string = '') => Object.fromEntries(
Array.from(
string.matchAll(/--(?<property>[^:]+?)\s*:\s*(?<value>.*?)\s*[;}]/gus),
([match, property, value]) => [property, value]
)
)
const css = `
:root {
--custom: property;
View import-es-module-from-data-uri.js
(async () => console.log(
(await import(`data:text/javascript,export default "Is this a safe way to eval?"`)).default
))()
View types-of-template-strings.js
`template string`
tagged`template string`
tagged('template', 'string')`with arguments before`
tagged`template string`('with', 'arguments', 'after')
View basic-custom-element.js
class CustomElement extends HTMLElement {
constructor() {
super()
this.shadow = this.attachShadow({mode: 'open'})
this.stylesheet = `
:host,
:host * {
box-sizing: border-box;
text-rendering: optimizeLegibility;