Skip to content

Commit

Permalink
refactor(dialog): reduce API surface
Browse files Browse the repository at this point in the history
  • Loading branch information
GregOnNet committed May 30, 2023
1 parent b1c43f3 commit 4d5da95
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 381 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export const Example01 = component$(() => {
return (
<PreviewCodeExample>
<div q:slot="actualComponent">
<Dialog.Root>
<Dialog.Element>
<Dialog.Trigger>
<button>Open Dialog</button>
</Dialog.Trigger>
<Dialog.Content>Hello World</Dialog.Content>
</Dialog.Root>
</Dialog.Element>
</div>

<div q:slot="codeExample">
Expand Down
12 changes: 0 additions & 12 deletions packages/kit-headless/src/components/dialog/dialog.close.tsx

This file was deleted.

This file was deleted.

60 changes: 2 additions & 58 deletions packages/kit-headless/src/components/dialog/dialog.content.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,5 @@
import {
$,
QwikMouseEvent,
Slot,
component$,
useContext,
useStylesScoped$,
useVisibleTask$,
} from '@builder.io/qwik';
import { dialogContext } from './dialog.context';
import { hasDialogBackdropBeenClicked } from './utils';
import { Slot, component$ } from '@builder.io/qwik';

export const Content = component$(() => {
useStylesScoped$(`
.full-screen {
width: 100vw;
height: 100vh;
}
`);

const context = useContext(dialogContext);
const props = context.state.props;

const closeOnBackdropClick$ = $(
(event: QwikMouseEvent<HTMLDialogElement, MouseEvent>) =>
hasDialogBackdropBeenClicked(event) ? context.close$() : Promise.resolve()
);

/**
*
* When dialog is closed by pressing the Escape-Key,
* we set the opened state to false.
*
*/
useVisibleTask$(() => {
const dialog = context.state.dialogRef.value;

if (!dialog) {
throw new Error(
'[Qwik UI Dialog]: Cannot update the Dialog state. <dialog>-Element not found.'
);
}

dialog.addEventListener('close', () => (context.state.opened = false));
});

return (
<dialog
{...props}
class={
context.state.props.fullScreen
? `${context.state.props.class} full-screen`
: `${context.state.props.class}`
}
ref={context.state.dialogRef}
onClick$={closeOnBackdropClick$}
>
<Slot />
</dialog>
);
return <Slot />;
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.full-screen {
width: 100vw;
height: 100vh;
}
113 changes: 113 additions & 0 deletions packages/kit-headless/src/components/dialog/dialog.element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {
$,
QwikMouseEvent,
Slot,
component$,
useSignal,
useStore,
useStylesScoped$,
useVisibleTask$,
} from '@builder.io/qwik';
import styles from './dialog.element.css?inline';
import { DialogState, RootProps } from './types';
import { hasDialogBackdropBeenClicked } from './utils';

export const Element = component$((props: RootProps) => {
useStylesScoped$(styles);

const dialogRef = useSignal<HTMLDialogElement>();

const state = useStore<DialogState>({
opened: false,
});

const open$ = $(() => {
const dialog = dialogRef.value;

if (!dialog) {
throw new Error(
'[Qwik UI Dialog]: Cannot open the Dialog. <dialog>-Element not found.'
);
}

dialog.showModal();
state.opened = true;
});

const close$ = $(() => {
const dialog = dialogRef.value;

if (!dialog) {
throw new Error(
'[Qwik UI Dialog]: Cannot close the Dialog. <dialog>-Element not found.'
);
}

dialog.close();
state.opened = false;
});

const closeOnBackdropClick$ = $(
(event: QwikMouseEvent<HTMLDialogElement, MouseEvent>) =>
hasDialogBackdropBeenClicked(event) ? close$() : Promise.resolve()
);

/**
*
* Share the public API of the Dialog if the dialog-caller is interested.
*
*/
useVisibleTask$(({ track }) => {
const opened = track(() => state.opened);

if (!props.ref) return;

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

/**
*
* Lock Scrolling on page when Dialog is opened.
*
*/
useVisibleTask$(({ track }) => {
const opened = track(() => state.opened);

const overflow = opened ? 'hidden' : '';

window.document.body.style.overflow = overflow;
});

/**
*
* When dialog is closed by pressing the Escape-Key,
* we set the opened state to false.
*
*/
useVisibleTask$(() => {
const dialog = dialogRef.value;

if (!dialog) {
throw new Error(
'[Qwik UI Dialog]: Cannot update the Dialog state. <dialog>-Element not found.'
);
}

dialog.addEventListener('close', () => (state.opened = false));
});

return (
<dialog
{...props}
class={props.fullScreen ? `${props.class} full-screen` : `${props.class}`}
ref={dialogRef}
onClick$={closeOnBackdropClick$}
>
<Slot />
</dialog>
);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Slot, component$, useStyles$ } from '@builder.io/qwik';

export const Actions = component$(() => {
export const Footer = component$(() => {
useStyles$(`
.dialog-actions {
position: sticky;
Expand All @@ -9,8 +9,8 @@ export const Actions = component$(() => {
`);

return (
<div class="dialog-actions">
<footer class="dialog-actions">
<Slot />
</div>
</footer>
);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Slot, component$, useStyles$ } from '@builder.io/qwik';

export const ContentTitle = component$(() => {
export const Header = component$(() => {
useStyles$(`
.dialog-content-title {
position: sticky;
Expand All @@ -9,8 +9,8 @@ export const ContentTitle = component$(() => {
`);

return (
<div class="dialog-content-title">
<header class="dialog-content-title">
<Slot />
</div>
</header>
);
});
92 changes: 0 additions & 92 deletions packages/kit-headless/src/components/dialog/dialog.root.tsx

This file was deleted.

Loading

0 comments on commit 4d5da95

Please sign in to comment.