@cssints/stuck 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/dist/index.d.mts +82 -0
- package/dist/index.mjs +57 -0
- package/dist/observer.d.mts +1 -0
- package/dist/observer.mjs +53 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexei Accio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,101 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cssints/stuck
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Style what is inside a sticky element while it is stuck, for [cssints](../../README.md): `sticky()` makes an element
|
|
4
|
+
sticky and a scroll-state container, `stuck(...)` wraps styles of its descendants in
|
|
5
|
+
`@container scroll-state(stuck: top)` (Chrome 133). Where a target lacks scroll-state queries, `stuck(...)` also emits
|
|
6
|
+
the same styles under an attribute that a tiny IntersectionObserver sets, in the way of
|
|
7
|
+
[bramus/sticky-observer](https://github.com/bramus/sticky-observer).
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { cn, color, hover } from "cssints" with { type: "cssints" };
|
|
11
|
+
import { sticky, stuck } from "@cssints/stuck" with { type: "cssints" };
|
|
12
|
+
|
|
13
|
+
export const header = sticky().top("0"); // <header class={header}>
|
|
14
|
+
export const title = cn(color("gray"), stuck(color("red"))); // <h1 class={title}>
|
|
15
|
+
export const footer = sticky().bottom(2);
|
|
16
|
+
export const link = stuck("bottom")(color("green"), hover(color("black")));
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```css
|
|
20
|
+
@layer _.k {
|
|
21
|
+
._stuck-core {
|
|
22
|
+
position: sticky;
|
|
23
|
+
container-type: scroll-state;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
@layer _.a {
|
|
27
|
+
._4k81c2 {
|
|
28
|
+
top: 0;
|
|
29
|
+
}
|
|
30
|
+
._kg6w97 {
|
|
31
|
+
bottom: 0.5rem;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
@layer _.b {
|
|
35
|
+
@container scroll-state(stuck: top) {
|
|
36
|
+
._it4l7h {
|
|
37
|
+
color: red;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/* only when a browserslist target lacks scroll-state queries */
|
|
41
|
+
:where(._g-stuck[data-stuck~="top"]) ._fec935 {
|
|
42
|
+
color: red;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`header` is `_stuck-core _g-stuck _4k81c2`, `title` holds both `_it4l7h` and `_fec935`.
|
|
48
|
+
|
|
49
|
+
## Browsers
|
|
50
|
+
|
|
51
|
+
- **Native**: `@container scroll-state()` and `container-type: scroll-state`, Chrome and Edge 133. Not in Firefox or
|
|
52
|
+
Safari yet.
|
|
53
|
+
- **Fallback**: `@cssints/stuck/observer` (about 1.3 kB minified, no imports) sets `data-stuck="top"` (a list of the
|
|
54
|
+
stuck edges) on each `sticky()` element while it is stuck, and the fallback rules read it. It checks
|
|
55
|
+
`CSS.supports("container-type", "scroll-state")` first and does nothing where the browser has the queries. Nothing to
|
|
56
|
+
import by hand: the runtime entry names it (`script`), so the module that uses `sticky()` imports it through
|
|
57
|
+
`virtual:cssints/runtime/stuck.observer` when a target lacks the queries; that module is empty when none does.
|
|
58
|
+
- The fallback rules are in the sheet only when a browserslist target lacks the queries (`chrome 133` alone gets the
|
|
59
|
+
`@container` rules only): `stuck()` asks `lacking()` of `cssints/plugin`, the same query and rules as the support
|
|
60
|
+
warnings. The kernel's support warnings (its `requires`, the queries, and its declaration
|
|
61
|
+
`container-type: scroll-state`) are covered by the observer, so a build for the default targets prints none.
|
|
62
|
+
- Checked in Chrome (native, and the observer with the queries cut out), Firefox and WebKit (the observer):
|
|
63
|
+
`bun run test:browsers`.
|
|
64
|
+
|
|
65
|
+
## Rules
|
|
66
|
+
|
|
67
|
+
- **`sticky()`** is `position: sticky` and `container-type: scroll-state`, with methods for the insets, each once, in any
|
|
68
|
+
order: `top()`, `right()`, `bottom()`, `left()`, a `<length-percentage>` (a lone number is a spacing step, 0.25rem).
|
|
69
|
+
Its class list also holds `_g-stuck`, the group marker the fallback rules read.
|
|
70
|
+
- **`stuck(...styles)`** applies the styles while the nearest `sticky()` ancestor is stuck to its top edge;
|
|
71
|
+
`stuck(side)(...styles)` to `top`, `right`, `bottom` or `left`. It is a condition: it counts as one (layer `_.b`),
|
|
72
|
+
wraps others (`stuck(hover(...))`) and goes inside them.
|
|
73
|
+
- **Descendants only.** A scroll-state query matches the descendants of the container, never the container itself (the
|
|
74
|
+
spec's rule), so `stuck()` styles go on an element inside the sticky one. To style the sticky box itself, put an inner
|
|
75
|
+
wrapper in it.
|
|
76
|
+
- **Conflicts.** `sticky()` sets `position` and `container-type`: one `cn()` with `position`, `container` or
|
|
77
|
+
`container-type` is a build error, derived from the kernel (written as styles): `cssints: stuck: kernel core sets
|
|
78
|
+
position, container-type (this style also sets position)`.
|
|
79
|
+
- **Errors**: a bad inset is the grammar's, in the types and at build time (`sticky.top: unexpected "red" in "red",
|
|
80
|
+
expected <length-percentage>`), a method twice is `cssints: sticky().top() is used twice`, and an unknown side is a type
|
|
81
|
+
error and `cssints: stuck: stuck() takes top, right, bottom or left, got "middle"`. Each points at the call.
|
|
82
|
+
|
|
83
|
+
## Limits
|
|
84
|
+
|
|
85
|
+
- **The fallback reads the insets once**, when it first sees the element: an inset that a media query changes later is
|
|
86
|
+
not followed. A sticky element taller than its scroll container is not reported stuck. Shadow roots are not crossed.
|
|
87
|
+
- The fallback's root is the nearest ancestor whose `overflow` is not `visible`, else the viewport; the native query
|
|
88
|
+
follows the real scroll container.
|
|
89
|
+
- No logical sides (`block-start`, `inline-end`), and no `scrollable` or `snapped` scroll-state queries.
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
cd packages/stuck && bun run test # fixtures through the engine (output, targets, errors, observer size)
|
|
93
|
+
cd packages/stuck && bun run typecheck # the type test
|
|
94
|
+
cd packages/stuck && bun run test:browsers # Chrome, Firefox and WebKit through playwright-core
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Credits
|
|
98
|
+
|
|
99
|
+
The fallback follows [bramus/sticky-observer](https://github.com/bramus/sticky-observer) (MIT, Bramus Van Damme): an
|
|
100
|
+
IntersectionObserver whose root is shrunk by the element's inset plus 1px. The observer is written here; no code is
|
|
101
|
+
copied.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Style, Wrap } from "cssints";
|
|
2
|
+
//#region src/index.d.ts
|
|
3
|
+
/** An edge a sticky element sticks to. */
|
|
4
|
+
export type Side = "top" | "right" | "bottom" | "left";
|
|
5
|
+
/** The scope of `sticky()`, for a scope that embeds it (its kernel `core`). */
|
|
6
|
+
export declare const stuckScope: import("cssints/plugin").Scope<"stuck", {
|
|
7
|
+
readonly "#core": {
|
|
8
|
+
readonly css: (css: import("cssints/plugin").Css) => Style[];
|
|
9
|
+
readonly requires: ["css.at-rules.container.scroll-state_queries"];
|
|
10
|
+
};
|
|
11
|
+
readonly "#observer": {
|
|
12
|
+
readonly polyfills: ["css.at-rules.container.scroll-state_queries", "css.properties.container-type.scroll-state"];
|
|
13
|
+
readonly script: "@cssints/stuck/observer";
|
|
14
|
+
};
|
|
15
|
+
readonly sticky: {
|
|
16
|
+
readonly uses: readonly ["core", "observer"];
|
|
17
|
+
readonly group: true;
|
|
18
|
+
readonly chain: {
|
|
19
|
+
readonly top: {
|
|
20
|
+
args: "<length-percentage>";
|
|
21
|
+
spacing: true;
|
|
22
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
23
|
+
};
|
|
24
|
+
readonly right: {
|
|
25
|
+
args: "<length-percentage>";
|
|
26
|
+
spacing: true;
|
|
27
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
28
|
+
};
|
|
29
|
+
readonly bottom: {
|
|
30
|
+
args: "<length-percentage>";
|
|
31
|
+
spacing: true;
|
|
32
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
33
|
+
};
|
|
34
|
+
readonly left: {
|
|
35
|
+
args: "<length-percentage>";
|
|
36
|
+
spacing: true;
|
|
37
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
}>;
|
|
42
|
+
/**
|
|
43
|
+
* `sticky().top("0")`: `position: sticky`, `container-type: scroll-state` and the insets of its methods (each once, a
|
|
44
|
+
* `<length-percentage>`, a lone number a spacing step). Its descendants take `stuck(...)` styles. It sets `position`
|
|
45
|
+
* and `container-type`, so those in the same `cn()` are a build error.
|
|
46
|
+
*/
|
|
47
|
+
export declare const sticky: () => import("cssints/plugin").Chain<"sticky", {
|
|
48
|
+
readonly top: {
|
|
49
|
+
args: "<length-percentage>";
|
|
50
|
+
spacing: true;
|
|
51
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
52
|
+
};
|
|
53
|
+
readonly right: {
|
|
54
|
+
args: "<length-percentage>";
|
|
55
|
+
spacing: true;
|
|
56
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
57
|
+
};
|
|
58
|
+
readonly bottom: {
|
|
59
|
+
args: "<length-percentage>";
|
|
60
|
+
spacing: true;
|
|
61
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
62
|
+
};
|
|
63
|
+
readonly left: {
|
|
64
|
+
args: "<length-percentage>";
|
|
65
|
+
spacing: true;
|
|
66
|
+
emit: (v: string) => (readonly [Side, string])[];
|
|
67
|
+
};
|
|
68
|
+
}, {}>;
|
|
69
|
+
/** The styles of a descendant of `sticky()` while it is stuck: to the top edge, or to the side given. */
|
|
70
|
+
export interface Stuck {
|
|
71
|
+
/** `stuck("bottom")(color("red"))`: while the sticky ancestor is stuck to that edge. */
|
|
72
|
+
(side: Side): Wrap;
|
|
73
|
+
/** `stuck(color("red"))`: while the sticky ancestor is stuck to the top edge. */
|
|
74
|
+
(...styles: Style[]): Style;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Styles of a descendant of `sticky()` while the sticky element is stuck: `stuck(...styles)` (top) or `stuck(side)(...styles)`.
|
|
78
|
+
*
|
|
79
|
+
* @returns {Style | Wrap} the styles under the condition, or the condition of that side
|
|
80
|
+
*/
|
|
81
|
+
export declare const stuck: Stuck;
|
|
82
|
+
//#endregion
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { attr, cn, container, within } from "cssints" with { type: "cssints" };
|
|
2
|
+
import { lacking, scope } from "cssints/plugin";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const SIDES = [
|
|
5
|
+
"top",
|
|
6
|
+
"right",
|
|
7
|
+
"bottom",
|
|
8
|
+
"left"
|
|
9
|
+
];
|
|
10
|
+
const QUERIES = "css.at-rules.container.scroll-state_queries";
|
|
11
|
+
const inset = (side) => ({
|
|
12
|
+
args: "<length-percentage>",
|
|
13
|
+
spacing: true,
|
|
14
|
+
emit: (v) => [[side, v]]
|
|
15
|
+
});
|
|
16
|
+
/** The scope of `sticky()`, for a scope that embeds it (its kernel `core`). */
|
|
17
|
+
const stuckScope = scope("stuck", {
|
|
18
|
+
"#core": {
|
|
19
|
+
css: (css) => [css.position("sticky"), css.containerType("scroll-state")],
|
|
20
|
+
requires: [QUERIES]
|
|
21
|
+
},
|
|
22
|
+
"#observer": {
|
|
23
|
+
polyfills: [QUERIES, "css.properties.container-type.scroll-state"],
|
|
24
|
+
script: "@cssints/stuck/observer"
|
|
25
|
+
},
|
|
26
|
+
sticky: {
|
|
27
|
+
uses: ["core", "observer"],
|
|
28
|
+
group: true,
|
|
29
|
+
chain: {
|
|
30
|
+
top: inset("top"),
|
|
31
|
+
right: inset("right"),
|
|
32
|
+
bottom: inset("bottom"),
|
|
33
|
+
left: inset("left")
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* `sticky().top("0")`: `position: sticky`, `container-type: scroll-state` and the insets of its methods (each once, a
|
|
39
|
+
* `<length-percentage>`, a lone number a spacing step). Its descendants take `stuck(...)` styles. It sets `position`
|
|
40
|
+
* and `container-type`, so those in the same `cn()` are a build error.
|
|
41
|
+
*/
|
|
42
|
+
const { sticky } = stuckScope.export();
|
|
43
|
+
const fallback = lacking(QUERIES).length > 0;
|
|
44
|
+
const under = (side) => (...styles) => cn(container(`scroll-state(stuck: ${side})`)(...styles), ...fallback ? [within(attr("data-stuck", side, "~="), "stuck")(...styles)] : []);
|
|
45
|
+
/**
|
|
46
|
+
* Styles of a descendant of `sticky()` while the sticky element is stuck: `stuck(...styles)` (top) or `stuck(side)(...styles)`.
|
|
47
|
+
*
|
|
48
|
+
* @returns {Style | Wrap} the styles under the condition, or the condition of that side
|
|
49
|
+
*/
|
|
50
|
+
const stuck = ((...args) => {
|
|
51
|
+
const [side] = args;
|
|
52
|
+
if (typeof side !== "string") return under("top")(...args);
|
|
53
|
+
if (SIDES.includes(side)) return under(side);
|
|
54
|
+
throw new Error(`cssints: stuck: stuck() takes top, right, bottom or left, got "${side}"`);
|
|
55
|
+
});
|
|
56
|
+
//#endregion
|
|
57
|
+
export { sticky, stuck, stuckScope };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/observer.ts
|
|
2
|
+
const SIDES = [
|
|
3
|
+
"top",
|
|
4
|
+
"right",
|
|
5
|
+
"bottom",
|
|
6
|
+
"left"
|
|
7
|
+
];
|
|
8
|
+
const watched = /* @__PURE__ */ new Map();
|
|
9
|
+
const scroller = (el) => {
|
|
10
|
+
const up = [el];
|
|
11
|
+
while (up.at(-1).parentElement) up.push(up.at(-1).parentElement);
|
|
12
|
+
return up.slice(1, -2).find((p) => /auto|scroll|hidden|overlay/.test(getComputedStyle(p).overflow)) ?? null;
|
|
13
|
+
};
|
|
14
|
+
const watch = (el) => {
|
|
15
|
+
const s = getComputedStyle(el);
|
|
16
|
+
const insets = SIDES.map((k) => s[k]);
|
|
17
|
+
const io = new IntersectionObserver(([e]) => {
|
|
18
|
+
const r = e.boundingClientRect;
|
|
19
|
+
const b = e.rootBounds;
|
|
20
|
+
const past = b && [
|
|
21
|
+
r.top < b.top,
|
|
22
|
+
r.right > b.right,
|
|
23
|
+
r.bottom > b.bottom,
|
|
24
|
+
r.left < b.left
|
|
25
|
+
];
|
|
26
|
+
const on = SIDES.filter((_, i) => e.isIntersecting && past?.[i] && insets[i] !== "auto");
|
|
27
|
+
if (on.length) el.setAttribute("data-stuck", on.join(" "));
|
|
28
|
+
else el.removeAttribute("data-stuck");
|
|
29
|
+
}, {
|
|
30
|
+
root: scroller(el),
|
|
31
|
+
rootMargin: insets.map((v) => v === "auto" ? "0px" : `${-1 - parseFloat(v)}px`).join(" "),
|
|
32
|
+
threshold: [0, 1]
|
|
33
|
+
});
|
|
34
|
+
io.observe(el);
|
|
35
|
+
watched.set(el, io);
|
|
36
|
+
};
|
|
37
|
+
const scan = () => {
|
|
38
|
+
for (const [el, io] of watched) if (!el.isConnected || !el.classList.contains("_stuck-core")) {
|
|
39
|
+
io.disconnect();
|
|
40
|
+
watched.delete(el);
|
|
41
|
+
el.removeAttribute("data-stuck");
|
|
42
|
+
}
|
|
43
|
+
for (const el of document.querySelectorAll("._stuck-core")) if (!watched.has(el)) watch(el);
|
|
44
|
+
};
|
|
45
|
+
if (typeof document !== "undefined" && !CSS.supports("container-type", "scroll-state")) {
|
|
46
|
+
scan();
|
|
47
|
+
new MutationObserver(scan).observe(document.documentElement, {
|
|
48
|
+
subtree: true,
|
|
49
|
+
childList: true,
|
|
50
|
+
attributeFilter: ["class"]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cssints/stuck",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Style a sticky element while it is stuck: @container scroll-state(stuck) natively, an IntersectionObserver fallback",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/alexeiaccio/css-in-ts-compiler.git",
|
|
9
|
+
"directory": "packages/stuck"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://cssints.dev",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"./src/observer.ts",
|
|
18
|
+
"./dist/observer.mjs",
|
|
19
|
+
"./test/fixtures/*"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./observer": {
|
|
27
|
+
"types": "./dist/observer.d.mts",
|
|
28
|
+
"default": "./dist/observer.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"cssints": "^0.2.0"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
}
|
|
40
|
+
}
|