@hashrytech/quick-components-kit 0.8.4 → 0.9.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/CHANGELOG.md +12 -0
- package/dist/actions/disable-scroll.d.ts +8 -0
- package/dist/actions/disable-scroll.js +37 -0
- package/dist/actions/on-keydown.d.ts +11 -0
- package/dist/actions/on-keydown.js +24 -0
- package/dist/drawer/Drawer.svelte +12 -31
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/modal/Modal.svelte +7 -24
- package/dist/modal/index.d.ts +1 -1
- package/dist/modal/index.js +1 -1
- package/dist/overlay/Overlay.svelte +6 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @hashrytech/quick-components-kit
|
|
2
2
|
|
|
3
|
+
## 0.9.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- feat: Adding new disable scroll and onkeyboard actions along with updates to Modal, Drawer and Overlay
|
|
8
|
+
|
|
9
|
+
## 0.8.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- fix: scroll disable fix for overlay component
|
|
14
|
+
|
|
3
15
|
## 0.8.4
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte action to disable page scroll while preserving scrollbar to avoid layout shift.
|
|
3
|
+
* Now supports toggling scroll lock on update.
|
|
4
|
+
*/
|
|
5
|
+
export declare function disableScroll(node: HTMLElement, disableBodyScroll?: boolean): {
|
|
6
|
+
update(newValue: boolean): void;
|
|
7
|
+
destroy(): void;
|
|
8
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte action to disable page scroll while preserving scrollbar to avoid layout shift.
|
|
3
|
+
* Now supports toggling scroll lock on update.
|
|
4
|
+
*/
|
|
5
|
+
export function disableScroll(node, disableBodyScroll = true) {
|
|
6
|
+
const scrollTop = window.scrollY;
|
|
7
|
+
const originalBodyPosition = document.body.style.position;
|
|
8
|
+
const originalBodyWidth = document.body.style.width;
|
|
9
|
+
const originalTop = document.body.style.top;
|
|
10
|
+
const originalOverflow = document.documentElement.style.overflowY;
|
|
11
|
+
function applyLock() {
|
|
12
|
+
document.body.style.top = `-${scrollTop}px`;
|
|
13
|
+
document.documentElement.style.overflowY = 'scroll';
|
|
14
|
+
document.body.style.position = 'fixed';
|
|
15
|
+
document.body.style.width = '100%';
|
|
16
|
+
}
|
|
17
|
+
function removeLock() {
|
|
18
|
+
document.body.style.position = originalBodyPosition;
|
|
19
|
+
document.body.style.width = originalBodyWidth;
|
|
20
|
+
document.body.style.top = originalTop;
|
|
21
|
+
document.documentElement.style.overflowY = originalOverflow;
|
|
22
|
+
window.scrollTo(0, scrollTop);
|
|
23
|
+
}
|
|
24
|
+
if (node && disableBodyScroll)
|
|
25
|
+
applyLock();
|
|
26
|
+
return {
|
|
27
|
+
update(newValue) {
|
|
28
|
+
if (newValue)
|
|
29
|
+
applyLock();
|
|
30
|
+
else
|
|
31
|
+
removeLock();
|
|
32
|
+
},
|
|
33
|
+
destroy() {
|
|
34
|
+
removeLock();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type KeyHandler = {
|
|
2
|
+
key: string;
|
|
3
|
+
callback: (event: KeyboardEvent) => void;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Action to call a function callback when a specific key is pressed.
|
|
7
|
+
*/
|
|
8
|
+
export declare function onKeydown(node: HTMLElement, { key, callback }: KeyHandler): {
|
|
9
|
+
update(newParams: KeyHandler): void;
|
|
10
|
+
destroy(): void;
|
|
11
|
+
} | undefined;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action to call a function callback when a specific key is pressed.
|
|
3
|
+
*/
|
|
4
|
+
export function onKeydown(node, { key, callback }) {
|
|
5
|
+
if (typeof window === 'undefined' || !node)
|
|
6
|
+
return;
|
|
7
|
+
const handle = (event) => {
|
|
8
|
+
if (event.key === key) {
|
|
9
|
+
callback(event);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
window.addEventListener('keydown', handle);
|
|
13
|
+
return {
|
|
14
|
+
update(newParams) {
|
|
15
|
+
window.removeEventListener('keydown', handle);
|
|
16
|
+
key = newParams.key;
|
|
17
|
+
callback = newParams.callback;
|
|
18
|
+
window.addEventListener('keydown', handle);
|
|
19
|
+
},
|
|
20
|
+
destroy() {
|
|
21
|
+
window.removeEventListener('keydown', handle);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
<script lang="ts" module>
|
|
2
|
-
import {
|
|
2
|
+
import { type Snippet } from 'svelte';
|
|
3
3
|
import type { ClassNameValue } from 'tailwind-merge';
|
|
4
|
-
import {
|
|
4
|
+
import { fly } from 'svelte/transition';
|
|
5
5
|
import {twMerge} from 'tailwind-merge';
|
|
6
|
-
|
|
6
|
+
import Overlay from '../overlay/Overlay.svelte';
|
|
7
|
+
import { onKeydown } from '../actions/on-keydown.js';
|
|
7
8
|
|
|
8
9
|
export type DrawerProps = {
|
|
9
10
|
open?: boolean;
|
|
@@ -36,46 +37,26 @@
|
|
|
36
37
|
bottom: "bottom-0 left-0 right-0 h-60",
|
|
37
38
|
};
|
|
38
39
|
|
|
39
|
-
const lockScroll = () => document.body.style.overflow = 'hidden';
|
|
40
|
-
const unlockScroll = () => document.body.style.overflow = '';
|
|
41
|
-
|
|
42
|
-
$effect(() => {
|
|
43
|
-
if (open && disableBodyScroll) lockScroll(); else unlockScroll();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
onMount(() => {
|
|
47
|
-
if(browser){
|
|
48
|
-
window.addEventListener('keydown', handleKeydown);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
onDestroy(() => {
|
|
53
|
-
if(browser){
|
|
54
|
-
window.removeEventListener('keydown', handleKeydown);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export function closeDrawer() {
|
|
60
|
-
open = false;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
40
|
function handleKeydown (event: { key: string; }) {
|
|
64
|
-
if(open && escapeKeyClose
|
|
41
|
+
if(open && escapeKeyClose) {
|
|
65
42
|
closeDrawer();
|
|
66
43
|
}
|
|
67
44
|
};
|
|
68
45
|
|
|
46
|
+
export function closeDrawer() {
|
|
47
|
+
open = false;
|
|
48
|
+
};
|
|
49
|
+
|
|
69
50
|
</script>
|
|
70
51
|
|
|
71
52
|
{#if open}
|
|
72
|
-
|
|
73
|
-
<div transition:fade={{duration: transitionDuration}} class={twMerge("fixed inset-0 bg-overlay-primary", overlayClasses)} role="presentation" onclick={() => open = false}></div>
|
|
53
|
+
<Overlay {transitionDuration} {disableBodyScroll} class={overlayClasses} onclick={() => open = false} />
|
|
74
54
|
|
|
75
55
|
<div role="dialog" aria-modal="true" aria-label={ariaLabel} tabindex="{open ? 0 : -1}" aria-hidden="{!open}"
|
|
76
56
|
class={twMerge("fixed flex flex-col items-center gap-2 bg-white outline-0 focus:outline-0 active:outline-focus-primary focus:outline-focus-primary overflow-y-auto", postionClasses[position], props.class)}
|
|
77
57
|
in:fly={transitionProperties}
|
|
78
|
-
out:fly={transitionProperties}
|
|
58
|
+
out:fly={transitionProperties}
|
|
59
|
+
use:onKeydown={{key: "Escape", callback: handleKeydown}}>
|
|
79
60
|
{@render children?.()}
|
|
80
61
|
</div>
|
|
81
62
|
{/if}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -7,4 +7,6 @@ export * from './hamburger-menu/index.js';
|
|
|
7
7
|
export * from './drawer/index.js';
|
|
8
8
|
export * from './modal/index.js';
|
|
9
9
|
export * from './overlay/index.js';
|
|
10
|
+
export * from './actions/disable-scroll.js';
|
|
11
|
+
export * from './actions/on-keydown.js';
|
|
10
12
|
// Add more components here...
|
package/dist/modal/Modal.svelte
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
import { fade } from 'svelte/transition';
|
|
5
5
|
import {twMerge} from 'tailwind-merge';
|
|
6
6
|
import { browser } from '$app/environment';
|
|
7
|
+
import Overlay from '../overlay/Overlay.svelte';
|
|
8
|
+
import { onKeydown } from '../actions/on-keydown.js';
|
|
7
9
|
|
|
8
10
|
export type ModalProps = {
|
|
9
11
|
open?: boolean;
|
|
@@ -19,27 +21,8 @@
|
|
|
19
21
|
</script>
|
|
20
22
|
|
|
21
23
|
<script lang="ts">
|
|
22
|
-
let {open=$bindable(false), escapeKeyClose=true, disableBodyScroll=true, overlayTransitionDuration=
|
|
23
|
-
|
|
24
|
-
const lockScroll = () => document.body.style.overflow = 'hidden';
|
|
25
|
-
const unlockScroll = () => document.body.style.overflow = '';
|
|
26
|
-
|
|
27
|
-
$effect(() => {
|
|
28
|
-
if (open && disableBodyScroll) lockScroll(); else unlockScroll();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
onMount(() => {
|
|
32
|
-
if(browser){
|
|
33
|
-
window.addEventListener('keydown', handleKeydown);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
onDestroy(() => {
|
|
38
|
-
if(browser){
|
|
39
|
-
window.removeEventListener('keydown', handleKeydown);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
24
|
+
let {open=$bindable(false), escapeKeyClose=true, disableBodyScroll=true, overlayTransitionDuration=100, ariaLabel="Modal", overlayClasses="", children, ...props}: ModalProps = $props();
|
|
25
|
+
|
|
43
26
|
export function closeModal() {
|
|
44
27
|
open = false;
|
|
45
28
|
};
|
|
@@ -53,11 +36,11 @@
|
|
|
53
36
|
</script>
|
|
54
37
|
|
|
55
38
|
{#if open}
|
|
56
|
-
|
|
57
|
-
<div transition:fade={{duration: overlayTransitionDuration}} class={twMerge("fixed inset-0 bg-overlay-primary", overlayClasses)} role="presentation" onclick={() => open = false}></div>
|
|
39
|
+
<Overlay transitionDuration={overlayTransitionDuration} {disableBodyScroll} class={overlayClasses} onclick={() => open = false} />
|
|
58
40
|
|
|
59
41
|
<div role="dialog" aria-modal="true" aria-label={ariaLabel} tabindex="{open ? 0 : -1}" aria-hidden="{!open}"
|
|
60
|
-
class={twMerge("fixed bg-white top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 outline-0 focus:outline-0 active:outline-focus-primary focus:outline-focus-primary overflow-y-auto w-full max-w-md h-96", props.class)}
|
|
42
|
+
class={twMerge("fixed bg-white rounded top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 outline-0 focus:outline-0 active:outline-focus-primary focus:outline-focus-primary overflow-y-auto w-full max-w-md h-96", props.class)}
|
|
43
|
+
use:onKeydown={{key: "Escape", callback: handleKeydown}}>
|
|
61
44
|
{@render children?.()}
|
|
62
45
|
</div>
|
|
63
46
|
{/if}
|
package/dist/modal/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as Modal } from './
|
|
1
|
+
export { default as Modal } from './Modal.svelte';
|
package/dist/modal/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as Modal } from './
|
|
1
|
+
export { default as Modal } from './Modal.svelte';
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
<script lang="ts" module>
|
|
2
|
-
import { type Snippet } from 'svelte';
|
|
2
|
+
import { onDestroy, onMount, type Snippet } from 'svelte';
|
|
3
3
|
import type { ClassNameValue } from 'tailwind-merge';
|
|
4
4
|
import { fade } from 'svelte/transition';
|
|
5
5
|
import {twMerge} from 'tailwind-merge';
|
|
6
|
+
import { disableScroll } from '../actions/disable-scroll.js';
|
|
6
7
|
|
|
7
8
|
export type OverlayProps = {
|
|
8
9
|
disableBodyScroll?: boolean;
|
|
@@ -16,16 +17,10 @@
|
|
|
16
17
|
</script>
|
|
17
18
|
|
|
18
19
|
<script lang="ts">
|
|
19
|
-
let {disableBodyScroll=true, transitionDuration=
|
|
20
|
-
|
|
21
|
-
const lockScroll = () => document.body.style.overflow = 'hidden';
|
|
22
|
-
const unlockScroll = () => document.body.style.overflow = '';
|
|
23
|
-
|
|
24
|
-
$effect(() => {
|
|
25
|
-
if (disableBodyScroll) lockScroll(); else unlockScroll();
|
|
26
|
-
});
|
|
27
|
-
|
|
20
|
+
let {disableBodyScroll=true, transitionDuration=100, ariaLabel="Overlay", onclick, children, ...props}: OverlayProps = $props();
|
|
28
21
|
</script>
|
|
29
22
|
|
|
30
|
-
<div transition:fade={{duration: transitionDuration}} class={twMerge("fixed top-0 left-0 right-0 bottom-0 bg-overlay-primary", props.class)} role="presentation" {onclick}
|
|
23
|
+
<div transition:fade={{duration: transitionDuration}} class={twMerge("fixed top-0 left-0 right-0 bottom-0 bg-overlay-primary", props.class)} role="presentation" {onclick}
|
|
24
|
+
use:disableScroll={disableBodyScroll}>
|
|
25
|
+
</div>
|
|
31
26
|
|