Skip to content

Commit 4d5da95

Browse files
GregOnNetGregor Woiwode
authored and
Gregor Woiwode
committed
refactor(dialog): reduce API surface
1 parent b1c43f3 commit 4d5da95

File tree

18 files changed

+238
-381
lines changed

18 files changed

+238
-381
lines changed

apps/website/src/routes/docs/headless/(components)/dialog/examples.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export const Example01 = component$(() => {
66
return (
77
<PreviewCodeExample>
88
<div q:slot="actualComponent">
9-
<Dialog.Root>
9+
<Dialog.Element>
1010
<Dialog.Trigger>
1111
<button>Open Dialog</button>
1212
</Dialog.Trigger>
1313
<Dialog.Content>Hello World</Dialog.Content>
14-
</Dialog.Root>
14+
</Dialog.Element>
1515
</div>
1616

1717
<div q:slot="codeExample">

packages/kit-headless/src/components/dialog/dialog.close.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/kit-headless/src/components/dialog/dialog.content-text.tsx

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,5 @@
1-
import {
2-
$,
3-
QwikMouseEvent,
4-
Slot,
5-
component$,
6-
useContext,
7-
useStylesScoped$,
8-
useVisibleTask$,
9-
} from '@builder.io/qwik';
10-
import { dialogContext } from './dialog.context';
11-
import { hasDialogBackdropBeenClicked } from './utils';
1+
import { Slot, component$ } from '@builder.io/qwik';
122

133
export const Content = component$(() => {
14-
useStylesScoped$(`
15-
.full-screen {
16-
width: 100vw;
17-
height: 100vh;
18-
}
19-
`);
20-
21-
const context = useContext(dialogContext);
22-
const props = context.state.props;
23-
24-
const closeOnBackdropClick$ = $(
25-
(event: QwikMouseEvent<HTMLDialogElement, MouseEvent>) =>
26-
hasDialogBackdropBeenClicked(event) ? context.close$() : Promise.resolve()
27-
);
28-
29-
/**
30-
*
31-
* When dialog is closed by pressing the Escape-Key,
32-
* we set the opened state to false.
33-
*
34-
*/
35-
useVisibleTask$(() => {
36-
const dialog = context.state.dialogRef.value;
37-
38-
if (!dialog) {
39-
throw new Error(
40-
'[Qwik UI Dialog]: Cannot update the Dialog state. <dialog>-Element not found.'
41-
);
42-
}
43-
44-
dialog.addEventListener('close', () => (context.state.opened = false));
45-
});
46-
47-
return (
48-
<dialog
49-
{...props}
50-
class={
51-
context.state.props.fullScreen
52-
? `${context.state.props.class} full-screen`
53-
: `${context.state.props.class}`
54-
}
55-
ref={context.state.dialogRef}
56-
onClick$={closeOnBackdropClick$}
57-
>
58-
<Slot />
59-
</dialog>
60-
);
4+
return <Slot />;
615
});

packages/kit-headless/src/components/dialog/dialog.context.tsx

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.full-screen {
2+
width: 100vw;
3+
height: 100vh;
4+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import {
2+
$,
3+
QwikMouseEvent,
4+
Slot,
5+
component$,
6+
useSignal,
7+
useStore,
8+
useStylesScoped$,
9+
useVisibleTask$,
10+
} from '@builder.io/qwik';
11+
import styles from './dialog.element.css?inline';
12+
import { DialogState, RootProps } from './types';
13+
import { hasDialogBackdropBeenClicked } from './utils';
14+
15+
export const Element = component$((props: RootProps) => {
16+
useStylesScoped$(styles);
17+
18+
const dialogRef = useSignal<HTMLDialogElement>();
19+
20+
const state = useStore<DialogState>({
21+
opened: false,
22+
});
23+
24+
const open$ = $(() => {
25+
const dialog = dialogRef.value;
26+
27+
if (!dialog) {
28+
throw new Error(
29+
'[Qwik UI Dialog]: Cannot open the Dialog. <dialog>-Element not found.'
30+
);
31+
}
32+
33+
dialog.showModal();
34+
state.opened = true;
35+
});
36+
37+
const close$ = $(() => {
38+
const dialog = dialogRef.value;
39+
40+
if (!dialog) {
41+
throw new Error(
42+
'[Qwik UI Dialog]: Cannot close the Dialog. <dialog>-Element not found.'
43+
);
44+
}
45+
46+
dialog.close();
47+
state.opened = false;
48+
});
49+
50+
const closeOnBackdropClick$ = $(
51+
(event: QwikMouseEvent<HTMLDialogElement, MouseEvent>) =>
52+
hasDialogBackdropBeenClicked(event) ? close$() : Promise.resolve()
53+
);
54+
55+
/**
56+
*
57+
* Share the public API of the Dialog if the dialog-caller is interested.
58+
*
59+
*/
60+
useVisibleTask$(({ track }) => {
61+
const opened = track(() => state.opened);
62+
63+
if (!props.ref) return;
64+
65+
props.ref.value = {
66+
opened,
67+
open$,
68+
close$,
69+
};
70+
});
71+
72+
/**
73+
*
74+
* Lock Scrolling on page when Dialog is opened.
75+
*
76+
*/
77+
useVisibleTask$(({ track }) => {
78+
const opened = track(() => state.opened);
79+
80+
const overflow = opened ? 'hidden' : '';
81+
82+
window.document.body.style.overflow = overflow;
83+
});
84+
85+
/**
86+
*
87+
* When dialog is closed by pressing the Escape-Key,
88+
* we set the opened state to false.
89+
*
90+
*/
91+
useVisibleTask$(() => {
92+
const dialog = dialogRef.value;
93+
94+
if (!dialog) {
95+
throw new Error(
96+
'[Qwik UI Dialog]: Cannot update the Dialog state. <dialog>-Element not found.'
97+
);
98+
}
99+
100+
dialog.addEventListener('close', () => (state.opened = false));
101+
});
102+
103+
return (
104+
<dialog
105+
{...props}
106+
class={props.fullScreen ? `${props.class} full-screen` : `${props.class}`}
107+
ref={dialogRef}
108+
onClick$={closeOnBackdropClick$}
109+
>
110+
<Slot />
111+
</dialog>
112+
);
113+
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Slot, component$, useStyles$ } from '@builder.io/qwik';
22

3-
export const Actions = component$(() => {
3+
export const Footer = component$(() => {
44
useStyles$(`
55
.dialog-actions {
66
position: sticky;
@@ -9,8 +9,8 @@ export const Actions = component$(() => {
99
`);
1010

1111
return (
12-
<div class="dialog-actions">
12+
<footer class="dialog-actions">
1313
<Slot />
14-
</div>
14+
</footer>
1515
);
1616
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Slot, component$, useStyles$ } from '@builder.io/qwik';
22

3-
export const ContentTitle = component$(() => {
3+
export const Header = component$(() => {
44
useStyles$(`
55
.dialog-content-title {
66
position: sticky;
@@ -9,8 +9,8 @@ export const ContentTitle = component$(() => {
99
`);
1010

1111
return (
12-
<div class="dialog-content-title">
12+
<header class="dialog-content-title">
1313
<Slot />
14-
</div>
14+
</header>
1515
);
1616
});

packages/kit-headless/src/components/dialog/dialog.root.tsx

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)