Skip to content

Commit fced932

Browse files
committed
fix: layer preset css
The client appends the preset to <head>, so it lands after the stylesheets of the page and source order made a preset you opt into one you could not override. It goes in the bongo cascade layer now: an unlayered rule beats a layered one whatever the order, so a rule of your own wins without !important. The preset carries no !important of its own, so a workaround that reached for one still wins. inject() had no test; it has five now.
1 parent 3282dbf commit fced932

5 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎docs/content/client-runtime.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ The view-transition preset:
7474
- A custom string only wraps the swap. Your CSS gives the rules.
7575
- Any falsy value turns transitions off.
7676

77+
A preset goes in the `bongo` [cascade layer](/css/#cascade-layers), so a rule of your own overrides it without `!important`. See [Turn on transitions](/view-transitions/#turn-on-transitions).
78+
7779
Opt a link out with `transition:disable`.
7880

7981
### prefetch

‎docs/content/css.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ A relative path resolves against the file that contains the `@import`, so an imp
3535

3636
A change to an imported file rebuilds each stylesheet that imports it, in `bongo build` and in `bongo dev`. A missing file fails the build, with an error that names the `@import` and the CSS file that contains it.
3737

38+
## Cascade layers
39+
40+
bongo puts the CSS it adds itself in the `bongo` layer. Today that is the [view-transition preset](/view-transitions/#turn-on-transitions) of the client runtime, which the runtime appends to `<head>` after the stylesheets of the page. An unlayered rule beats a layered one whatever the order, so your own CSS overrides it without `!important`.
41+
42+
Name the layer to order it against your own:
43+
44+
```css [assets/app.css]
45+
@layer bongo, reset, theme;
46+
```
47+
48+
Your own rules stay unlayered unless you put them in a layer, so most sites need no such line.
49+
3850
## CSS modules
3951

4052
A `.module.css` file is a CSS module: LightningCSS gives each of its class names a hash, so that it cannot clash with a class of another stylesheet. Read the new names in a template with `$assets.classes()`:

‎docs/content/view-transitions.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ Give the `transitions` option to the client runtime:
2424
| any other string | Wraps the swap only. Your CSS gives the rules. |
2525
| any falsy value | Turns transitions off. This is the default. |
2626

27+
A preset goes in the `bongo` [cascade layer](/css/#cascade-layers), so a rule of your own overrides it without `!important`:
28+
29+
```css [assets/app.css]
30+
::view-transition-old(root),
31+
::view-transition-new(root) {
32+
animation: none;
33+
}
34+
```
35+
2736
See [ClientOptions](/client-runtime/#clientoptions).
2837

2938
## Name elements

‎packages/bongo/src/client/preset.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
import { describe, expect, test } from 'bun:test';
1+
/// <reference lib="dom" />
2+
import { afterEach, describe, expect, test } from 'bun:test';
3+
import { Window } from 'happy-dom';
24
import * as preset from './preset';
35

6+
let win: Window | undefined;
7+
8+
function mount(): void {
9+
let w: Window = win = new Window({ url: 'https://example.com/' });
10+
w.document.write('<!doctype html><html><head></head><body></body></html>');
11+
Reflect.set(globalThis, 'document', w.document);
12+
}
13+
14+
function tags(): string[] {
15+
return [ ...document.head.querySelectorAll('style[data-bongo-preset]') ].map(el => el.outerHTML);
16+
}
17+
418
describe('preset.resolve()', () => {
519
test('true resolves to "fade"', () => {
620
expect(preset.resolve(true)).toBe('fade');
@@ -30,3 +44,44 @@ describe('preset.resolve()', () => {
3044
expect(preset.resolve('my-custom-preset')).toBe('my-custom-preset');
3145
});
3246
});
47+
48+
describe('preset.inject()', () => {
49+
afterEach(() => {
50+
win?.close();
51+
win = undefined;
52+
});
53+
54+
test('wraps the preset in the "bongo" layer, so a page rule beats it', () => {
55+
mount();
56+
preset.inject('fade');
57+
let [ tag ] = tags();
58+
expect(tag).toStartWith('<style data-bongo-preset="fade">@layer bongo{');
59+
expect(tag).toEndWith('}</style>');
60+
expect(tag).toInclude('::view-transition-old(root){animation-name:bongo-fade-out');
61+
});
62+
63+
test('"slide" is layered too', () => {
64+
mount();
65+
preset.inject('slide');
66+
expect(tags()[0]).toInclude('@layer bongo{@keyframes bongo-slide-in');
67+
});
68+
69+
test('"initial" adds no CSS', () => {
70+
mount();
71+
preset.inject('initial');
72+
expect(tags()).toEqual([]);
73+
});
74+
75+
test('an unknown preset adds no CSS', () => {
76+
mount();
77+
preset.inject('my-custom-preset');
78+
expect(tags()).toEqual([]);
79+
});
80+
81+
test('a second call adds no second tag', () => {
82+
mount();
83+
preset.inject('fade');
84+
preset.inject('fade');
85+
expect(tags()).toHaveLength(1);
86+
});
87+
});

‎packages/bongo/src/client/preset.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ export function resolve(opt: Options): Preset | (string & {}) | undefined {
2525
else if (opt) return opt;
2626
}
2727

28+
/**
29+
* The preset goes in the `bongo` cascade layer. The tag is appended to
30+
* `<head>`, so it is always after the stylesheets of the page, and source
31+
* order would else make a preset that you opt into one you cannot override.
32+
* An unlayered rule beats a layered one whatever the order, so your own CSS
33+
* wins without `!important`.
34+
*/
2835
export function inject(name: Preset | (string & {})): void {
2936
let css = CSS[name];
3037
if (!css) return; // 'initial' or unknown custom preset — user handles CSS
3138
if (document.querySelector(`style[data-bongo-preset="${name}"]`)) return;
3239
let el = document.createElement('style');
3340
el.setAttribute('data-bongo-preset', name);
34-
el.textContent = css;
41+
el.textContent = `@layer bongo{${css}}`;
3542
document.head.appendChild(el);
3643
}

0 commit comments

Comments
 (0)