and I work at Google.
If we grow the language in these few ways [that let the users grow it themselves], then we will not need to grow it in a hundred other ways; the users can take on the rest of the task.
Guy Steele, Growing a Language
Might say yes to any, but must say no to all.
The Houdini TF is part of the CSSWG, dedicated to making CSS user-extensible.
Extension language is usually JS; occasionally simple parts may be "pure CSS".
html {
--header-color: #006;
--main-color: #06c;
--accent-color: #c06;
}
a { color: var(--main-color); }
a:visited { color: var(--accent-color); }
h1 {
color: var(--header-color);
background: linear-gradient(var(--main-color), #0000);
}
Custom Properties originally sold as "variables" (it's in the spec name!), for organizing repeated values throughout a spec.
Actually, custom properties are an extension point — they can take anything, and be processed by JS.
However, Level 1 spec is weak, optimized for "variables" usage, and intentionally simple, for easy first implementation.
Properties & Values spec starts fixing custom properties:
CSS.registerProperty({
name: "--stop-color",
syntax: "",
inherits: false,
initialValue: "transparent"
});
.button {
--stop-color: red;
background: linear-gradient(var(--stop-color), black);
transition: --stop-color 1s;
}
.button:hover {
--stop-color: green;
}
Missing from current level is easy way to respond to a custom property, or changes to one.
In other words, custom properties still act liek variables, not like author-created properties.
Lack is intentional, again, because simplicity makes it easier to implement correctly at first.
Level 2 will provide hooks into at least the "computed value" transformation, where most CSS magic happens.
CSS.registerProperty({
name: "--translate-x",
type: "<length-percentage>",
initial: "0px",
inherits: false
});
...
CSS.registerComputedValueHook({
inputProperties: ["--translate-*", "--scale-*", "--rotate", "transform"],
outputProperties: ["transform"],
computedValue: function(input, output) {
output.transform = CSSTransformValue(
input.get('--translate-x'),
...,
input.get('transform'));
}
});
#myElement {
--translate-x: 5px;
--translate-y: 10px;
--rotate: 10deg;
--scale-x: 25;
--scale-y: 25;
}
.foobar {
--rotate: 20deg;
}
Previous example invoked CSSTransformValue
— this is a new Typed OM value.
Current CSS "Object Model" is string-based, which is dumb:
Real performance implications in hot code!
The Typed OM spec is a stopgap solution to this: https://drafts.css-houdini.org/css-typed-om/
Creates a bunch of objects to represent CSS values, rather than strings.
Uses a Map-like interface (actually a MultiMap) for style blocks.
This is a "bedrock" spec - all the Houdini specs depend on it.
In some cases, not the most convenient: "rotate(45deg)"
is easier to write than CSSRotation(45)
.
But in others, much easier: let newWidth = oldWidth.add(someLength);
, regardless of what the units are.
Hoping for future JS improvements that would make a v2 much easier: suffix constructors (let length = 5em;
), operator overloading (let newWidth = oldWidth + someLength;
), etc.
Speed gains, tho, are very real — animating many transforms becomes much faster in our tests.
Finally getting to the actual extensions!
Custom Paint is author-defined "paint sources", usable anywhere CSS expects an <image>
: background
, list-style
, border-image
, content
, etc.
For example, Lea Verou's plea for conic gradients wants syntax like:
padding: 5em; /* size */
background: conic-gradient(gold 40%, #f06 0);
border-radius: 50%; /* make it round */
CSS.registerPaint('circle', class {
// Syntax is too complex for v1 args, so just "*" for now
static inputArguments = ["*"];
paint(ctx, geom, props, args) {
// Determine the center point and cover radius.
const x = geom.width / 2;
const y = geom.height / 2;
const radius = Math.sqrt(x*x + y*y);
// Draw the arcs
let angleSoFar = CSSAngleValue(0);
for(arg of args) {
let stop = /* parse each arg into color/angle pair */;
ctx.fillStyle = stop.color;
ctx.beginPath();
ctx.arc(x, y, radius, angleSoFar.degrees, stop.angle.degrees, false);
ctx.fill();
angleSoFar = stop.angle;
}
}
});
padding: 5em; /* size */
background: paint(conic-gradient, gold 40%, #f06 0);
border-radius: 50%; /* make it round */
In future, when we have custom functions, a tiny bit more code will allow:
background: --conic-gradient(gold 40%, #f06 0);
Custom Paint (and most other new Houdini APIs) is run in a "worklet", a lighter version of a Web Worker.
Separate process, restricted environment, might be killed and restarted at any time. Can't save state between calls.
These restrictions necessary for efficiently calling into JS in the middle of layout/painting/etc.
Flexbox and Grid took so long, but we're so hungry for better layout, they're still useful.
Turnover cycle on new layout specs is way too long.
Like Guy Steele said, any individual new layout type might be reasonable, but we can't add all the useful layout types people might want to use.
Existing layout modes are responsive to "natural" size of children.
This is hard to measure today.
Hacks include using an off-screen iframe to measure the laid-out size of elements without disturbing the current page.
(Read the Flexbox Layout Algorithm for examples.)
Existing layout modes know how to tell their parents about their own "natural" sizes, which enables auto-sizing, used everywhere.
Impossible to do well today without hacky/slow resize handlers.
(Read the CSS Sizing spec for details.)
Layout is heavily optimized today, and switching between browser and JS repeatedly is expensive.
Solution, like Custom Paint, is a "worklet" for your layout code. Much cheaper and easier than JS in general.
Custom Layout is much more complicated and hard to write than Custom Paint.
Example I'm about to show is very preliminary, and over-simplified in dangerous ways, but shows roughly how layout code will look.
Better, more thorough examples upcoming in the spec.
registerLayout('block-like', class {
async layout(space, children, styleMap, breakToken) {
let blockOffset = 0;
const inlineSize = resolveInlineSize(space, styleMap);
const childFragments = [];
// ... snip ...
for (let child of children) {
let fragment = await child.doLayout(childSpace);
// Position the fragment in a block like manner and center
fragment.blockOffset = blockOffset;
fragment.inlineOffset =
Math.max(0, (inlineSize - fragment.inlineSize) / 2);
blockOffset += fragment.blockSize;
}
// ... snip ...
return {
blockSize: blockOffset,
inlineSize: inlineSize,
fragments: childFragments,
};
}});
Ask Me Anything - I Am A Spec Writer