@svelte-atoms/core 1.0.0-alpha.23 → 1.0.0-alpha.24

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.
@@ -1,70 +1,70 @@
1
- <script lang="ts" generics="E extends keyof HTMLElementTagNameMap = 'div', B extends Base = Base">
2
- import type { HTMLAttributes } from 'svelte/elements';
3
- import type { Base, ComponentBase, HtmlAtomProps, SnippetBase } from './types';
4
- import { RootBond } from '../root';
5
- import { HtmlElement } from '../element';
6
- import { cn, toClassValue, type ClassValue } from '../../utils';
7
- import { getPreset } from '../../context';
8
- import type { PresetModuleName } from '../../context/preset.svelte';
9
-
10
- type Element = HTMLElementTagNameMap[E];
11
-
12
- const rootBond = RootBond.get();
13
-
14
- let {
15
- class: klass = '',
16
- as = 'div',
17
- base = undefined,
18
- preset: presetKey = undefined,
19
- bond = undefined,
20
- children = undefined,
21
- ...restProps
22
- }: HtmlAtomProps<E, B> & HTMLAttributes<Element> = $props();
23
-
24
- const preset = $derived(
25
- presetKey ? getPreset(presetKey as PresetModuleName)?.apply?.(bond, [bond]) : undefined
26
- );
27
-
28
- const _klass = $derived(compilePresetPlaceholder(klass));
29
-
30
- const _base = $derived(base ?? preset?.base);
31
- const _as = $derived(as ?? preset?.as);
32
- const _restProps = $derived.by(() => {
33
- const { class: klass, base, as, ...restPresetProps } = preset ?? {};
34
- return { ...restPresetProps, ...restProps };
35
- });
36
-
37
- const isSnippet = $derived(typeof _base === 'function' && _base.length === 1 && !_base.prototype);
38
-
39
- const snippet = $derived(_base as SnippetBase);
40
-
41
- const atom = rootBond?.state?.props?.renderers?.html ?? HtmlElement;
42
-
43
- const Component = $derived(base ?? atom) as ComponentBase;
44
-
45
- function compilePresetPlaceholder(klass: ClassValue): ReturnType<typeof toClassValue> {
46
- if (Array.isArray(klass)) {
47
- return klass.map((k) => compilePresetPlaceholder(k));
48
- }
49
-
50
- const compiled = toClassValue.apply(bond, [klass, bond]);
51
-
52
- if (Array.isArray(compiled)) {
53
- return compiled.map((c) => compilePresetPlaceholder(c));
54
- }
55
-
56
- if (typeof compiled === 'string') {
57
- return compiled.replace('$preset', cn(preset?.class));
58
- }
59
-
60
- return compiled;
61
- }
62
- </script>
63
-
64
- {#if isSnippet}
65
- {@render snippet({ class: _klass, as: _as, base: _base, children, ..._restProps })}
66
- {:else}
67
- <Component class={_klass} as={_as} {..._restProps}>
68
- {@render children?.()}
69
- </Component>
70
- {/if}
1
+ <script lang="ts" generics="E extends keyof HTMLElementTagNameMap = 'div', B extends Base = Base">
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import type { Base, ComponentBase, HtmlAtomProps, SnippetBase } from './types';
4
+ import { RootBond } from '../root';
5
+ import { HtmlElement } from '../element';
6
+ import { cn, toClassValue, type ClassValue } from '../../utils';
7
+ import { getPreset } from '../../context';
8
+ import type { PresetModuleName } from '../../context/preset.svelte';
9
+
10
+ type Element = HTMLElementTagNameMap[E];
11
+
12
+ const rootBond = RootBond.get();
13
+
14
+ let {
15
+ class: klass = '',
16
+ as = 'div',
17
+ base = undefined,
18
+ preset: presetKey = undefined,
19
+ bond = undefined,
20
+ children = undefined,
21
+ ...restProps
22
+ }: HtmlAtomProps<E, B> & HTMLAttributes<Element> = $props();
23
+
24
+ const preset = $derived(
25
+ presetKey ? getPreset(presetKey as PresetModuleName)?.apply?.(bond, [bond]) : undefined
26
+ );
27
+
28
+ const _klass = $derived(compilePresetPlaceholder(klass));
29
+
30
+ const _base = $derived(base ?? preset?.base);
31
+ const _as = $derived(as ?? preset?.as);
32
+ const _restProps = $derived.by(() => {
33
+ const { class: klass, base, as, ...restPresetProps } = preset ?? {};
34
+ return { ...restPresetProps, ...restProps };
35
+ });
36
+
37
+ const isSnippet = $derived(typeof _base === 'function' && _base.length === 1 && !_base.prototype);
38
+
39
+ const snippet = $derived(_base as SnippetBase);
40
+
41
+ const atom = rootBond?.state?.props?.renderers?.html ?? HtmlElement;
42
+
43
+ const Component = $derived(base ?? atom) as ComponentBase;
44
+
45
+ function compilePresetPlaceholder(klass: ClassValue): ReturnType<typeof toClassValue> {
46
+ if (Array.isArray(klass)) {
47
+ return klass.map((k) => compilePresetPlaceholder(k));
48
+ }
49
+
50
+ const compiled = toClassValue.apply(bond, [klass, bond]);
51
+
52
+ if (Array.isArray(compiled)) {
53
+ return compiled.map((c) => compilePresetPlaceholder(c));
54
+ }
55
+
56
+ if (typeof compiled === 'string') {
57
+ return compiled.replace('$preset', cn(preset?.class));
58
+ }
59
+
60
+ return compiled;
61
+ }
62
+ </script>
63
+
64
+ {#if isSnippet}
65
+ {@render snippet({ class: _klass, as: _as, base: _base, children, ..._restProps })}
66
+ {:else}
67
+ <Component class={_klass} as={_as} {..._restProps}>
68
+ {@render children?.()}
69
+ </Component>
70
+ {/if}
@@ -1,79 +1,79 @@
1
- <script lang="ts" generics="E extends keyof HTMLElementTagNameMap = 'div', B extends Base = Base">
2
- import type { HTMLAttributes } from 'svelte/elements';
3
- import { InputBond, InputState, type InputStateProps } from './bond.svelte';
4
- import { defineProperty, defineState, toClassValue } from '../../utils';
5
- import {
6
- HtmlAtom,
7
- type ElementType,
8
- type HtmlAtomProps,
9
- type Base
10
- } from '../atom';
11
-
12
- type Element = ElementType<E>;
13
-
14
- let {
15
- class: klass = '',
16
- value,
17
- checked = undefined,
18
- files = [],
19
- preset = 'input',
20
- children = undefined,
21
- factory = _factory,
22
- ...restProps
23
- }: HtmlAtomProps<E, B> & HTMLAttributes<Element> = $props();
24
-
25
- const bondProps = defineState<InputStateProps>([
26
- defineProperty(
27
- 'value',
28
- () => value,
29
- (v) => {
30
- value = v;
31
- }
32
- ),
33
-
34
- defineProperty(
35
- 'checked',
36
- () => checked,
37
- (v) => {
38
- checked = v;
39
- }
40
- ),
41
-
42
- defineProperty(
43
- 'files',
44
- () => files,
45
- (v) => {
46
- files = [...v];
47
- }
48
- )
49
- ]);
50
- const bond = _factory(bondProps).share();
51
-
52
- const rootProps = $derived({
53
- ...bond.root(),
54
- ...restProps
55
- });
56
-
57
- function _factory(props: typeof bondProps) {
58
- const bondState = new InputState(() => props);
59
-
60
- return new InputBond(bondState);
61
- }
62
-
63
- export function getBond() {
64
- return bond;
65
- }
66
- </script>
67
-
68
- <HtmlAtom
69
- {preset}
70
- class={[
71
- 'border-border text-foreground bg-input relative flex h-10 items-center overflow-hidden rounded-md border',
72
- '$preset',
73
- klass
74
- ]}
75
- {bond}
76
- {...rootProps}
77
- >
78
- {@render children?.({ input: bond })}
79
- </HtmlAtom>
1
+ <script lang="ts" generics="E extends keyof HTMLElementTagNameMap = 'div', B extends Base = Base">
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import { InputBond, InputState, type InputStateProps } from './bond.svelte';
4
+ import { defineProperty, defineState, toClassValue } from '../../utils';
5
+ import {
6
+ HtmlAtom,
7
+ type ElementType,
8
+ type HtmlAtomProps,
9
+ type Base
10
+ } from '../atom';
11
+
12
+ type Element = ElementType<E>;
13
+
14
+ let {
15
+ class: klass = '',
16
+ value,
17
+ checked = undefined,
18
+ files = [],
19
+ preset = 'input',
20
+ children = undefined,
21
+ factory = _factory,
22
+ ...restProps
23
+ }: HtmlAtomProps<E, B> & HTMLAttributes<Element> = $props();
24
+
25
+ const bondProps = defineState<InputStateProps>([
26
+ defineProperty(
27
+ 'value',
28
+ () => value,
29
+ (v) => {
30
+ value = v;
31
+ }
32
+ ),
33
+
34
+ defineProperty(
35
+ 'checked',
36
+ () => checked,
37
+ (v) => {
38
+ checked = v;
39
+ }
40
+ ),
41
+
42
+ defineProperty(
43
+ 'files',
44
+ () => files,
45
+ (v) => {
46
+ files = [...v];
47
+ }
48
+ )
49
+ ]);
50
+ const bond = _factory(bondProps).share();
51
+
52
+ const rootProps = $derived({
53
+ ...bond.root(),
54
+ ...restProps
55
+ });
56
+
57
+ function _factory(props: typeof bondProps) {
58
+ const bondState = new InputState(() => props);
59
+
60
+ return new InputBond(bondState);
61
+ }
62
+
63
+ export function getBond() {
64
+ return bond;
65
+ }
66
+ </script>
67
+
68
+ <HtmlAtom
69
+ {preset}
70
+ class={[
71
+ 'border-border text-foreground bg-input relative flex h-10 items-center overflow-hidden rounded-md border',
72
+ '$preset',
73
+ klass
74
+ ]}
75
+ {bond}
76
+ {...rootProps}
77
+ >
78
+ {@render children?.({ input: bond })}
79
+ </HtmlAtom>
@@ -1,38 +1,38 @@
1
- <script module>
2
- import { defineMeta } from '@storybook/addon-svelte-csf';
3
- import { Input as AInput } from '.';
4
- import Root from '../root/root.svelte';
5
- import { Label } from '../label';
6
-
7
- // More on how to set up stories at: https://storybook.js.org/docs/writing-stories
8
- const { Story } = defineMeta({
9
- title: 'Atoms/Input',
10
- // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
11
-
12
- parameters: {
13
- // More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
14
- layout: 'fullscreen'
15
- },
16
- args: {}
17
- });
18
- </script>
19
-
20
- <script lang="ts">
21
- </script>
22
-
23
- <Story name="Input" args={{}}>
24
- <Root class="p-4">
25
- <div class="flex flex-col">
26
- <Label for="price-input">Price</Label>
27
- <AInput.Root>
28
- <AInput.Icon class="text-foreground box-content px-0">$</AInput.Icon>
29
- <AInput.Value id="price-input" class="border-border box-content border-x px-2 py-2">
30
- <!-- -->
31
- </AInput.Value>
32
- <AInput.Icon class="text-foreground box-content px-2">.00</AInput.Icon>
33
-
34
- <AInput.Placeholder class="text-foreground/20 pl-2">Hello World</AInput.Placeholder>
35
- </AInput.Root>
36
- </div>
37
- </Root>
38
- </Story>
1
+ <script module>
2
+ import { defineMeta } from '@storybook/addon-svelte-csf';
3
+ import { Input as AInput } from '.';
4
+ import Root from '../root/root.svelte';
5
+ import { Label } from '../label';
6
+
7
+ // More on how to set up stories at: https://storybook.js.org/docs/writing-stories
8
+ const { Story } = defineMeta({
9
+ title: 'Atoms/Input',
10
+ // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
11
+
12
+ parameters: {
13
+ // More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
14
+ layout: 'fullscreen'
15
+ },
16
+ args: {}
17
+ });
18
+ </script>
19
+
20
+ <script lang="ts">
21
+ </script>
22
+
23
+ <Story name="Input" args={{}}>
24
+ <Root class="p-4">
25
+ <div class="flex flex-col">
26
+ <Label for="price-input">Price</Label>
27
+ <AInput.Root>
28
+ <AInput.Icon class="text-foreground box-content px-0">$</AInput.Icon>
29
+ <AInput.Value id="price-input" class="border-border box-content border-x px-2 py-2">
30
+ <!-- -->
31
+ </AInput.Value>
32
+ <AInput.Icon class="text-foreground box-content px-2">.00</AInput.Icon>
33
+
34
+ <AInput.Placeholder class="text-foreground/20 pl-2">Hello World</AInput.Placeholder>
35
+ </AInput.Root>
36
+ </div>
37
+ </Root>
38
+ </Story>
@@ -1,103 +1,121 @@
1
- <script module lang="ts">
2
- export type RootPortals = 'root.l0' | 'root.l1' | 'root.l2' | 'root.l3';
3
- </script>
4
-
5
- <script lang="ts">
6
- import { cn, defineState, defineProperty } from '../../utils';
7
- import { RootBond, RootBondState, type RootStateProps } from './bond.svelte';
8
- import { Portal, ActivePortal, Portals } from '../portal';
9
- import { Stack } from '../stack';
10
- import { HtmlAtom } from '../atom';
11
- import { HtmlElement, MathmlElement, SvgElement } from '../element';
12
-
13
- let { class: klass = '', children = undefined, portals = undefined, ...restProps } = $props();
14
-
15
- let html: typeof HtmlElement | undefined = HtmlElement;
16
- let svg: typeof SvgElement | undefined = undefined;
17
- let mathml: typeof MathmlElement | undefined = undefined;
18
-
19
- type Renderers = {
20
- html?: typeof HtmlElement;
21
- svg?: typeof SvgElement;
22
- mathml?: typeof MathmlElement;
23
- };
24
-
25
- const renderers = defineState<Renderers>([
26
- defineProperty('html', () => {
27
- if (!html) {
28
- import('../element/html-element.svelte').then((mod) => {
29
- html = mod.default;
30
- });
31
- }
32
-
33
- return html;
34
- }),
35
- defineProperty('svg', () => {
36
- if (!svg) {
37
- import('../element/svg-element.svelte').then((mod) => {
38
- svg = mod.default;
39
- });
40
- }
41
-
42
- return svg;
43
- }),
44
- defineProperty('mathml', () => {
45
- if (!mathml) {
46
- import('../element/mathml-element.svelte').then((mod) => {
47
- mathml = mod.default;
48
- });
49
- }
50
-
51
- return mathml;
52
- })
53
- ]);
54
-
55
- const bondProps = defineState<RootStateProps>([defineProperty('renderers', () => renderers)]);
56
-
57
- const bondState = new RootBondState(() => bondProps);
58
- const bond = new RootBond(bondState).share();
59
- </script>
60
-
61
- <HtmlAtom
62
- {@attach (node) => {
63
- bond.rootElement = node;
64
- }}
65
- preset="root"
66
- class={cn(
67
- 'atom-root bg-background text-foreground relative flex w-full flex-1 flex-col items-start font-sans',
68
- '$preset',
69
- klass
70
- )}
71
- {...restProps}
72
- >
73
- <Portals id="root">
74
- {#if portals}
75
- {@render portals?.()}
76
- {:else}
77
- <Stack.Root
78
- preset="root.portals"
79
- class={['portals pointer-events-none absolute inset-0 z-1 overflow-hidden', '$preset']}
80
- >
81
- <Portal.Outer base={Stack.Item} id="root.l0">
82
- <Portal.Inner />
83
- </Portal.Outer>
84
-
85
- <Portal.Outer base={Stack.Item} id="root.l1">
86
- <Portal.Inner />
87
- </Portal.Outer>
88
-
89
- <Portal.Outer base={Stack.Item} id="root.l2">
90
- <Portal.Inner />
91
- </Portal.Outer>
92
-
93
- <Portal.Outer base={Stack.Item} id="root.l3">
94
- <Portal.Inner />
95
- </Portal.Outer>
96
- </Stack.Root>
97
- {/if}
98
-
99
- <ActivePortal id="root.l0">
100
- {@render children?.()}
101
- </ActivePortal>
102
- </Portals>
103
- </HtmlAtom>
1
+ <script module lang="ts">
2
+ export type RootPortals = 'root.l0' | 'root.l1' | 'root.l2' | 'root.l3';
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import { cn, defineState, defineProperty } from '../../utils';
7
+ import { RootBond, RootBondState, type RootStateProps } from './bond.svelte';
8
+ import { Portal, ActivePortal, Portals } from '../portal';
9
+ import { Stack } from '../stack';
10
+ import { HtmlAtom } from '../atom';
11
+ import { HtmlElement, MathmlElement, SvgElement } from '../element';
12
+
13
+ let {
14
+ class: klass = '',
15
+ base = undefined,
16
+ children = undefined,
17
+ portals = undefined,
18
+ ...restProps
19
+ } = $props();
20
+
21
+ let html: typeof HtmlElement | undefined = HtmlElement;
22
+ let svg: typeof SvgElement | undefined = undefined;
23
+ let mathml: typeof MathmlElement | undefined = undefined;
24
+
25
+ type Renderers = {
26
+ html?: typeof HtmlElement;
27
+ svg?: typeof SvgElement;
28
+ mathml?: typeof MathmlElement;
29
+ };
30
+
31
+ const renderers = defineState<Renderers>([
32
+ defineProperty('html', () => {
33
+ if (!html) {
34
+ import('../element/html-element.svelte').then((mod) => {
35
+ html = mod.default;
36
+ });
37
+ }
38
+
39
+ return html;
40
+ }),
41
+ defineProperty('svg', () => {
42
+ if (!svg) {
43
+ import('../element/svg-element.svelte').then((mod) => {
44
+ svg = mod.default;
45
+ });
46
+ }
47
+
48
+ return svg;
49
+ }),
50
+ defineProperty('mathml', () => {
51
+ if (!mathml) {
52
+ import('../element/mathml-element.svelte').then((mod) => {
53
+ mathml = mod.default;
54
+ });
55
+ }
56
+
57
+ return mathml;
58
+ })
59
+ ]);
60
+
61
+ const bondProps = defineState<RootStateProps>([defineProperty('renderers', () => renderers)]);
62
+
63
+ const bondState = new RootBondState(() => bondProps);
64
+ const bond = new RootBond(bondState).share();
65
+ </script>
66
+
67
+ <Portals id="root">
68
+ <HtmlAtom
69
+ {@attach (node) => {
70
+ bond.rootElement = node;
71
+ }}
72
+ {base}
73
+ preset="root"
74
+ class={cn(
75
+ 'atom-root bg-background text-foreground relative flex w-full flex-1 flex-col items-start font-sans',
76
+ '$preset',
77
+ klass
78
+ )}
79
+ {...restProps}
80
+ >
81
+ {#if portals}
82
+ {@render portals?.()}
83
+ {:else}
84
+ <Portal.Outer
85
+ base={Stack.Item}
86
+ id="root.l0"
87
+ class="pointer-events-none absolute inset-0 z-10 overflow-hidden"
88
+ >
89
+ <Portal.Inner />
90
+ </Portal.Outer>
91
+
92
+ <Portal.Outer
93
+ base={Stack.Item}
94
+ id="root.l1"
95
+ class="pointer-events-none absolute inset-0 z-10 overflow-hidden"
96
+ >
97
+ <Portal.Inner />
98
+ </Portal.Outer>
99
+
100
+ <Portal.Outer
101
+ base={Stack.Item}
102
+ id="root.l2"
103
+ class="pointer-events-none absolute inset-0 z-10 overflow-hidden"
104
+ >
105
+ <Portal.Inner />
106
+ </Portal.Outer>
107
+
108
+ <Portal.Outer
109
+ base={Stack.Item}
110
+ id="root.l3"
111
+ class="pointer-events-none absolute inset-0 z-10 overflow-hidden"
112
+ >
113
+ <Portal.Inner />
114
+ </Portal.Outer>
115
+ {/if}
116
+
117
+ <ActivePortal id="root.l0">
118
+ {@render children?.()}
119
+ </ActivePortal>
120
+ </HtmlAtom>
121
+ </Portals>
@@ -1,6 +1,7 @@
1
1
  export type RootPortals = 'root.l0' | 'root.l1' | 'root.l2' | 'root.l3';
2
2
  declare const Root: import("svelte").Component<{
3
3
  class?: string;
4
+ base?: any;
4
5
  children?: any;
5
6
  portals?: any;
6
7
  } & Record<string, any>, {}, "">;
@@ -1,2 +1,5 @@
1
1
  export { colorScheme, type ColorScheme } from './color-scheme.svelte';
2
+ export { reducedMotion } from './reduced-motion.svelte';
3
+ export { container } from './container.svelte';
4
+ export { type Viewport, viewport } from './viewport.svelte';
2
5
  export { mounted } from './lifecycles.svelte';
@@ -1,2 +1,5 @@
1
1
  export { colorScheme } from './color-scheme.svelte';
2
+ export { reducedMotion } from './reduced-motion.svelte';
3
+ export { container } from './container.svelte';
4
+ export { viewport } from './viewport.svelte';
2
5
  export { mounted } from './lifecycles.svelte';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * A rune that tracks the user's motion preferences via the `prefers-reduced-motion` media query.
3
+ *
4
+ * @returns An object with a `current` getter that returns `true` if the user prefers reduced motion, `false` otherwise.
5
+ *
6
+ * @example
7
+ * ```svelte
8
+ * <script>
9
+ * import { reducedMotion } from '@svelte-atoms/core';
10
+ *
11
+ * const motion = reducedMotion();
12
+ * </script>
13
+ *
14
+ * {#if motion.current}
15
+ * <div>Animations disabled</div>
16
+ * {:else}
17
+ * <div class="animated">Animations enabled</div>
18
+ * {/if}
19
+ * ```
20
+ */
21
+ export declare function reducedMotion(): {
22
+ readonly current: boolean;
23
+ };