Skip to content

Commit 1630974

Browse files
committed
feat: Add computedStyles to retrieve all computed styles for element. Useful to resolve CSS variable values or working with <canvas>
1 parent cc15cc5 commit 1630974

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

.changeset/olive-laws-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@layerstack/svelte-actions': patch
3+
---
4+
5+
feat: Add `computedStyles` to retrieve all computed styles for element. Useful to resolve CSS variable values or working with `<canvas>`

packages/svelte-actions/src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export * from './portal.js';
99
export * from './scroll.js';
1010
export * from './spotlight.js';
1111
export * from './sticky.js';
12-
export * from './styleProps.js';
12+
export * from './styles.js';

packages/svelte-actions/src/lib/styleProps.ts renamed to packages/svelte-actions/src/lib/styles.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Action } from 'svelte/action';
22
import { entries, keys } from '@layerstack/utils';
33

44
type CSSProps = { [key: string]: string | number | boolean | null | undefined };
5-
65
export const styleProps: Action<HTMLElement, CSSProps> = (node, props) => {
76
entries(props ?? {}).forEach(([key, value]) => {
87
// Ignore if null or undefined
@@ -35,3 +34,12 @@ export const styleProps: Action<HTMLElement, CSSProps> = (node, props) => {
3534
},
3635
};
3736
};
37+
38+
type ComputedStylesCallback = (styles: CSSStyleDeclaration) => void;
39+
export const computedStyles: Action<HTMLElement | SVGElement, ComputedStylesCallback> = (
40+
node,
41+
callback
42+
) => {
43+
const computedStyles: CSSStyleDeclaration = window.getComputedStyle(node);
44+
callback(computedStyles);
45+
};

sites/docs/src/routes/_NavMenu.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
'scroll',
1717
'spotlight',
1818
'sticky',
19-
'styleProps',
19+
'styles',
2020
'table',
2121
];
2222

sites/docs/src/routes/docs/svelte-actions/styleProps/+page.svelte renamed to sites/docs/src/routes/docs/svelte-actions/styles/+page.svelte

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
<script lang="ts">
22
import { TextField } from 'svelte-ux';
3-
import { styleProps } from '@layerstack/svelte-actions';
3+
import { computedStyles, styleProps } from '@layerstack/svelte-actions';
44
55
import Preview from '$docs/Preview.svelte';
66
import Code from '$docs/Code.svelte';
7+
import Json from '$docs/Json.svelte';
78
9+
let _styles: CSSStyleDeclaration;
810
let background = '#ddd';
911
let border = '1px solid #aaa';
1012
</script>
1113

1214
<h1>Usage</h1>
1315

1416
<Code
15-
source={`import { styleProps } from '@layerstack/svelte-actions';`}
17+
source={`import { computedStyles, styleProps } from '@layerstack/svelte-actions';`}
1618
language="javascript"
1719
class="mb-4"
1820
/>
1921

22+
<h2>
23+
computedStyles <small>
24+
Retrieve all computed styles for element. Useful to resolve CSS variable values or working with
25+
`canvas`.
26+
</small>
27+
</h2>
28+
29+
<Preview>
30+
<div class="bg-primary" use:computedStyles={(styles) => (_styles = styles)}></div>
31+
<Json value={_styles} defaultExpandedPaths={[]} />
32+
</Preview>
33+
34+
<h2>styleProps <small>Reactively set style properties using a single object.</small></h2>
35+
2036
<Preview>
2137
{@const styles = { '--background': background, '--border': border }}
2238
<div class="grid gap-4" use:styleProps={styles}>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import source from '$svelte-actions/styleProps.ts?raw';
1+
import source from '$svelte-actions/styles.ts?raw';
22
import pageSource from './+page.svelte?raw';
33

44
export async function load() {
55
return {
66
meta: {
77
source,
88
pageSource,
9-
description: 'Reactively set style properties using a single object.',
9+
description: 'Actions to conveniently work with CSS styles',
1010
},
1111
};
1212
}

0 commit comments

Comments
 (0)