lapikit 0.0.0-insiders.c8d4fe2 → 0.0.0-insiders.cfec658

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 +1,2 @@
1
1
  export * from '../components/accordion/accordion.svelte.js';
2
+ export * from '../internal/mediaQueries.js';
@@ -1 +1,2 @@
1
1
  export * from '../components/accordion/accordion.svelte.js';
2
+ export * from '../internal/mediaQueries.js';
@@ -4,11 +4,21 @@
4
4
  colorSchemeSystem,
5
5
  modalOpen,
6
6
  setOpenModal,
7
- updateThemeStore
7
+ updateThemeStore,
8
+ viewportWidth
8
9
  } from '../../stores/index.js';
9
10
  import type { Snippet } from 'svelte';
10
11
  let { children }: { children: Snippet } = $props();
11
12
 
13
+ // states
14
+ let innerWidth = $state(0);
15
+
16
+ $effect(() => {
17
+ if (BROWSER) {
18
+ viewportWidth.set(innerWidth);
19
+ }
20
+ });
21
+
12
22
  $effect.pre(() => {
13
23
  if (!BROWSER) return;
14
24
  // system
@@ -31,6 +41,8 @@
31
41
  });
32
42
  </script>
33
43
 
44
+ <svelte:window bind:innerWidth />
45
+
34
46
  {@render children?.()}
35
47
 
36
48
  <!-- svelte-ignore a11y_no_static_element_interactions -->
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Non-reactive media queries function
3
+ * Use this in JavaScript code when you need a one-time check
4
+ * @param {...Array<["min"|"max", string]>} args - ex: [ ["min", "xs"], ["max", "lg"] ]
5
+ * @returns {boolean}
6
+ */
7
+ export declare function mediaQueries(...args: Array<['min' | 'max', string]> | ['min' | 'max', string]): boolean;
8
+ /**
9
+ * Reactive media queries that returns a store
10
+ * Use this in Svelte templates for automatic reactivity
11
+ * @param {...Array<["min"|"max", string]>} args - ex: [ ["min", "xs"], ["max", "lg"] ]
12
+ * @returns {import('svelte/store').Readable<boolean>}
13
+ */
14
+ export declare function mediaQueriesReactive(...args: Array<['min' | 'max', string]> | ['min' | 'max', string]): import("svelte/store").Readable<boolean>;
15
+ /**
16
+ * Convenient function for direct use in Svelte templates
17
+ * Returns a readable store that updates automatically
18
+ * Usage: $mediaQuery(['min', 'md']) in template
19
+ */
20
+ export declare const mediaQuery: typeof mediaQueriesReactive;
@@ -0,0 +1,88 @@
1
+ import { get, derived } from 'svelte/store';
2
+ import { breakpoints } from '../stores/breakpoints.js';
3
+ import { viewportWidth } from '../stores/viewports.js';
4
+ // Converts rem/em/px to number of pixels
5
+ function toPx(value) {
6
+ if (typeof value === 'number')
7
+ return value;
8
+ if (typeof value === 'string') {
9
+ if (value.endsWith('rem')) {
10
+ return parseFloat(value) * 16;
11
+ }
12
+ if (value.endsWith('em')) {
13
+ return parseFloat(value) * 16;
14
+ }
15
+ if (value.endsWith('px')) {
16
+ return parseFloat(value);
17
+ }
18
+ return parseFloat(value);
19
+ }
20
+ return 0;
21
+ }
22
+ /**
23
+ * Non-reactive media queries function
24
+ * Use this in JavaScript code when you need a one-time check
25
+ * @param {...Array<["min"|"max", string]>} args - ex: [ ["min", "xs"], ["max", "lg"] ]
26
+ * @returns {boolean}
27
+ */
28
+ export function mediaQueries(...args) {
29
+ const bp = get(breakpoints);
30
+ const width = get(viewportWidth) || (typeof window !== 'undefined' ? window.innerWidth : 0);
31
+ // Parse arguments
32
+ let queries = [];
33
+ if (Array.isArray(args[0]) && typeof args[0][0] === 'string') {
34
+ queries = args;
35
+ }
36
+ else if (typeof args[0] === 'string') {
37
+ queries = [args];
38
+ }
39
+ let result = true;
40
+ for (const [type, key] of queries) {
41
+ const value = bp[key];
42
+ const px = toPx(value);
43
+ if (type === 'min') {
44
+ result = result && width >= px;
45
+ }
46
+ else if (type === 'max') {
47
+ result = result && width <= px;
48
+ }
49
+ }
50
+ return result;
51
+ }
52
+ /**
53
+ * Reactive media queries that returns a store
54
+ * Use this in Svelte templates for automatic reactivity
55
+ * @param {...Array<["min"|"max", string]>} args - ex: [ ["min", "xs"], ["max", "lg"] ]
56
+ * @returns {import('svelte/store').Readable<boolean>}
57
+ */
58
+ export function mediaQueriesReactive(...args) {
59
+ return derived([breakpoints, viewportWidth], ([$breakpoints, $viewportWidth]) => {
60
+ const width = $viewportWidth || (typeof window !== 'undefined' ? window.innerWidth : 0);
61
+ // Parse arguments same way as non-reactive version
62
+ let queries = [];
63
+ if (Array.isArray(args[0]) && typeof args[0][0] === 'string') {
64
+ queries = args;
65
+ }
66
+ else if (typeof args[0] === 'string') {
67
+ queries = [args];
68
+ }
69
+ let result = true;
70
+ for (const [type, key] of queries) {
71
+ const value = $breakpoints[key];
72
+ const px = toPx(value);
73
+ if (type === 'min') {
74
+ result = result && width >= px;
75
+ }
76
+ else if (type === 'max') {
77
+ result = result && width <= px;
78
+ }
79
+ }
80
+ return result;
81
+ });
82
+ }
83
+ /**
84
+ * Convenient function for direct use in Svelte templates
85
+ * Returns a readable store that updates automatically
86
+ * Usage: $mediaQuery(['min', 'md']) in template
87
+ */
88
+ export const mediaQuery = mediaQueriesReactive;
@@ -9,3 +9,5 @@ export declare function setOpenModal(state: boolean | 'persistent'): void;
9
9
  export declare const pushModal: (id: string) => void;
10
10
  export declare const popModal: (id: string) => void;
11
11
  export * from './breakpoints.js';
12
+ export * from './viewports.js';
13
+ export { mediaQueries, mediaQueriesReactive, mediaQuery } from '../internal/mediaQueries.js';
@@ -46,3 +46,5 @@ export const popModal = (id) => {
46
46
  });
47
47
  };
48
48
  export * from './breakpoints.js';
49
+ export * from './viewports.js';
50
+ export { mediaQueries, mediaQueriesReactive, mediaQuery } from '../internal/mediaQueries.js';
@@ -0,0 +1,2 @@
1
+ import { type Writable } from 'svelte/store';
2
+ export declare const viewportWidth: Writable<number>;
@@ -0,0 +1,5 @@
1
+ // store viewport and screen dimensions
2
+ import { writable } from 'svelte/store';
3
+ // presets
4
+ const refWidth = 0;
5
+ export const viewportWidth = writable(refWidth);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lapikit",
3
- "version": "0.0.0-insiders.c8d4fe2",
3
+ "version": "0.0.0-insiders.cfec658",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"