Skip to content

Commit 2c93081

Browse files
committed
feat(dialog): make tailwind example work
1 parent b29d0ac commit 2c93081

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import { component$, Slot } from '@builder.io/qwik';
1+
import { component$, Slot, useSignal } from '@builder.io/qwik';
22
import { Button, Dialog } from '@qwik-ui/tailwind';
33
import { PreviewCodeExample } from '../../../../../components/preview-code-example/preview-code-example';
44

55
export const Example01 = component$(() => {
6+
const dialogRef = useSignal<Dialog.DialogRef>();
7+
68
return (
79
<PreviewCodeExample>
810
<div q:slot="actualComponent">
9-
<Dialog.Root>
10-
<Dialog.Trigger>
11-
<button>Open Dialog</button>
12-
</Dialog.Trigger>
11+
<Button onClick$={() => dialogRef.value?.open()}>Open Dialog</Button>
12+
13+
<Dialog.Root ref={dialogRef}>
14+
<Dialog.Header>
15+
<h2 id="dialog-heading">Hello 👋</h2>
16+
</Dialog.Header>
1317
<Dialog.Content>
14-
<Dialog.ContentTitle>Hello 👋</Dialog.ContentTitle>
15-
<Dialog.ContentText>This is a simple dialog.</Dialog.ContentText>
16-
<Dialog.Actions>
17-
<Dialog.Close>
18-
<Button>Close</Button>
19-
</Dialog.Close>
20-
</Dialog.Actions>
18+
<p id="dialog-text">I am a simple Dialog.</p>
2119
</Dialog.Content>
20+
<Dialog.Footer>
21+
<button onClick$={() => dialogRef.value?.close()}>
22+
Close Dialog
23+
</button>
24+
</Dialog.Footer>
2225
</Dialog.Root>
2326
</div>
2427

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
component$,
66
useSignal,
77
useStylesScoped$,
8-
useTask$,
98
useVisibleTask$,
109
} from '@builder.io/qwik';
1110
import styles from './dialog.root.css?inline';
@@ -45,7 +44,7 @@ export const Root = component$((props: RootProps) => {
4544
* Share the public API of the Dialog if the dialog-caller is interested.
4645
*
4746
*/
48-
useTask$(() => {
47+
useVisibleTask$(() => {
4948
if (!props.ref) return;
5049

5150
props.ref.value = { opened, open, close };

packages/kit-tailwind/src/components/dialog/dialog.tsx

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
useComputed$,
55
useSignal,
66
useStyles$,
7+
useVisibleTask$,
78
} from '@builder.io/qwik';
8-
import { Dialog, DialogRef } from '@qwik-ui/headless';
9+
import { Dialog } from '@qwik-ui/headless';
10+
11+
export type DialogRef = Dialog.DialogRef;
912

1013
export const Root = component$((props: Dialog.RootProps) => {
1114
/**
@@ -33,38 +36,42 @@ export const Root = component$((props: Dialog.RootProps) => {
3336
}
3437
`);
3538

36-
const dialog = useSignal<DialogRef>();
39+
const dialogRef = useSignal<Dialog.DialogRef>();
3740

3841
const dialogClass = useComputed$(() => {
39-
const clazz = dialog.value?.opened ? 'modal modal-open' : 'modal';
42+
const dialog = dialogRef.value;
43+
console.log('as;dlaksd;ka', dialogRef.value);
44+
const clazz = dialog?.opened.value ? 'modal modal-open' : 'modal';
4045

4146
return clazz;
4247
});
4348

49+
useVisibleTask$(({ track }) => {
50+
// HACK: If the dialogRef is not tracked, the ref is not propagated sometimes.
51+
// Normally, we should not need to track the dialogRef.
52+
track(() => dialogRef.value);
53+
54+
if (!props.ref) return;
55+
56+
props.ref.value = dialogRef.value;
57+
});
58+
4459
return (
4560
<>
4661
<Dialog.Root
47-
class={`${dialogClass.value} modal--backdrop`}
4862
{...props}
49-
ref={dialog}
63+
class={`${dialogClass.value} modal--backdrop`}
64+
ref={dialogRef}
5065
>
51-
<Slot />
66+
<div class="modal-box">
67+
<Slot />
68+
</div>
5269
</Dialog.Root>
5370
</>
5471
);
5572
});
5673

57-
export const Content = component$(() => {
58-
return (
59-
<Dialog.Content>
60-
<div class="modal-box">
61-
<Slot />
62-
</div>
63-
</Dialog.Content>
64-
);
65-
});
66-
67-
export const ContentTitle = component$(() => {
74+
export const Header = component$(() => {
6875
return (
6976
<Dialog.Header>
7077
<h3 class="font-bold text-lg">
@@ -73,18 +80,9 @@ export const ContentTitle = component$(() => {
7380
</Dialog.Header>
7481
);
7582
});
83+
export const Content = Dialog.Content;
7684

77-
export const ContentText = component$(() => {
78-
return (
79-
<Dialog.Content>
80-
<p class="py-4">
81-
<Slot />
82-
</p>
83-
</Dialog.Content>
84-
);
85-
});
86-
87-
export const Actions = component$(() => {
85+
export const Footer = component$(() => {
8886
return (
8987
<Dialog.Footer>
9088
<div class="modal-action">

0 commit comments

Comments
 (0)