-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(dialog): reduce API surface
- Loading branch information
Showing
18 changed files
with
238 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
packages/kit-headless/src/components/dialog/dialog.close.tsx
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
packages/kit-headless/src/components/dialog/dialog.content-text.tsx
This file was deleted.
Oops, something went wrong.
60 changes: 2 additions & 58 deletions
60
packages/kit-headless/src/components/dialog/dialog.content.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />; | ||
}); |
4 changes: 0 additions & 4 deletions
4
packages/kit-headless/src/components/dialog/dialog.context.tsx
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
packages/kit-headless/src/components/dialog/dialog.element.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
packages/kit-headless/src/components/dialog/dialog.element.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
packages/kit-headless/src/components/dialog/dialog.root.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.