Repository navigation
Description
Merges two roadmap entries into one config surface: Scoped CSS
(docs/design/extensions_roadmap.mdsection 2, upstream
django-components #1230)
and Inline CSS (roadmap section 3, upstream
django-components #1147).
The two features interact semantically (what "inline my CSS" means depends
on whether the CSS is scoped), so they should present one mental model to
the user even though they can ship in stages.
What each feature is
- Scoped: isolate a component's CSS to the elements it renders (the Vue
model): stamp a per-component data attribute on the component's own
elements (stopping at nested component boundaries, with the Vue rule for
in-template slot fills), rewrite each top-level selector to require that
attribute. Changes the reach of the CSS. - Inline: apply the collected CSS to the rendered HTML as
style="..."
attributes on matching elements (the
css_inlinesense: walk the
HTML, match every rule, write the declarations onto matching tags).
Changes the delivery of the CSS, not its meaning. Primary use cases:
HTML emails (clients strip<style>), embeddable fragments/widgets
injected into pages citry does not control, CSP contexts that forbid
style tags.
Note: the roadmap's Inline CSS row currently describes it as inlining "into
the page (a <style> block)", which is not what upstream
#1147
tracks; correct the row when the roadmap is next touched.
The semantics, settled by one principle
Inlining is a delivery-mechanism change and must not change which
elements the CSS affects. From that, the combination matrix follows:
scoped |
inline |
Behavior |
|---|---|---|
| off | off | Today's default: the component's CSS is a page-global stylesheet |
| on | off | Selectors rewritten + attributes stamped; still a <style> tag, but rules only match the component's own elements |
| off | on | The CSS is page-global, so faithful inlining applies it page-wide: every matching element in the rendered tree gets the declarations, including elements deeper in child components and outside the component entirely |
| on | on | Rules only match the component's scope-marked elements, so inlining lands exactly on the component's own rendered HTML. The email/widget sweet spot |
Two clarifications this matrix encodes:
- Unscoped component CSS does not merely "propagate to children": a
<style>block is page-global. It also matches sibling components,
other instances, and surrounding page HTML. So the answer to "should
inlinego deeper than the component's own HTML?" is: with unscoped CSS,
yes, all the way (anything else would silently change styling);
containment to the component's own elements is exactly whatscoped
expresses. No third semantics is needed. - Because scoping rewrites the CSS and stamps the HTML before inlining
runs, the inliner needs no knowledge of scoping at all: it faithfully
applies whatever CSS exists to whatever HTML matches, and scope-rewritten
selectors are self-limiting. The mechanical dependency between the two
features is only ordering (scope first, inline last); the reason to merge
them is the product surface, not the machinery.
Where each runs (the seams exist today)
- Scoped, two halves:
- CSS half: rewrite selectors once per class. Rides the existing
on_css_loadedmap hook, which under the asset compiler design
(docs/design/asset_compiler.mdsection 6) fires on already-compiled
CSS, so SCSS-authored styles scope correctly too. Deterministic
(scope id derives fromclass_id), so it composes with the content
caches. - HTML half: stamp the scope attribute on the component's rendered
elements per render. The root-marker machinery,
on_template_compiled, andon_slot_renderedseams are built
(roadmap section 2 row). The selector-rewrite primitive remains a
candidate for the Rusthtml_transformlayer so other bindings can
reuse it.
- CSS half: rewrite selectors once per class. Rides the existing
- Inline runs at serialization, on the final page HTML. This is the
conclusion upstream
#1147
reached the hard way (django-components lacked a whole-output hook and
sketched root-detection viaComponent.parent); citry does not need the
workaround becauseon_serializealready exists (final HTML in, final
HTML out). The styles to inline are obtainable via theon_dependencies
emit hook (mutablescripts/styleslists at emission): pull the
styles marked for inlining out of normal emission, apply them to the
HTML, and emit the non-inlinable residue (media queries, pseudo-classes,
keyframes, which cannot live instyle=attributes) as a<style>
block, which is standardcss_inlinebehavior. - Consequence for the roadmap's section 4 hook list: like the asset
compiler before it, the inline-CSS feature does not need the
on_template_postprocesspost-render hook (serialize covers it),
which leaves the reactive extension's whole-HTML wrap as that hook's
only remaining candidate consumer.
Proposed config surface
One nested config (name open: Css / CssConfig), staged delivery:
class EmailCard(Component):
class Css:
scoped = True # per component: reach of this component's CSS
inline = True # honored when this component is the render root
css = """
.card { color: #333; }
"""scopedis naturally per component.inlineis naturally per rendered tree (the pass runs on the final
HTML). Upstream #1147 says "should be set on the root component";
the citry equivalent: the flag takes effect when the component is the
render root (which covers the email and fragment cases, where one
component is the document), plus anextensions_defaultsglobal for
site-wide inlining. Setting it on a non-root component does nothing
except when that component is rendered standalone; document this rather
than warn, since "renders as an email body AND as a page fragment" is a
legitimate dual use.- Staging: the scoped half is the roadmap's build-now item and can ship
first;inlinearrives later on the same config class (the per-extension
config schema grows field by field). Merging the surface does not
couple the schedules.
Implementation notes and caveats
- Do not own the inliner:
css_inline
(Rust core, maintained, official Python bindings) is the engine upstream
#1147 already points at; it handles cascade resolution among the inlined
rules and keeps non-inlinable rules in a style block. Optional extra per
thecitry[ext-...]/lazy-import conventions. - Cascade caveat to document:
style=attributes outrank stylesheets,
so inlining can change the effective styling when other page CSS was
intended to override component CSS (and!importantinterplay is its own
corner). This is inherent to CSS inlining, not a citry bug; the docs
should say "inline changes delivery, and delivery has cascade
consequences at the page boundary". css_data()variables: CSS custom properties set via the
[data-ccss-<hash>]variables stylesheets are legal instyle=
attributes and inherit; whether the inliner should fold them or leave
the variables stylesheet as residue needs a decision during design
(emails likely want them folded, since some clients ignore custom
properties entirely).- Base URL:
url(...)references in inlined declarations may need
absolutizing for emails; upstream sketches aget_request_domain
override, citry's equivalent seam is the mount contract /
Citry.build_url. Design during build. - Prior art warning carried over from the roadmap: the old
django-components working tree contains acss_scope.pydraft and a
scope_css()regex-based rewriter; both are broken drafts
("build properly, do not port"), useful only as intent records (the Vue
slot-fill rule, the:rootopt-out idea, the tinycss2 pointer).
Relationship to other work
- Asset compiler (#10):
upstream of both halves; scoping sees compiled CSS because the compiler
runs beforeon_css_loaded. - Minifier extension (#35):
a minifier riding the futureon_css_postprocess(pre-cache) would see
scoped-rewritten CSS if scoping stays onon_css_loaded(load-time);
ordering between the two CSS transforms is a design point for whichever
lands second. - Upstream: django-components #1230
(scoped CSS),
django-components #1147
(inline CSS viacss_inline).
Activity