-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4930605
commit 123edb5
Showing
13 changed files
with
228 additions
and
4 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { memo } from 'react' | ||
|
||
|
||
export const DialogProvider = memo(() => { | ||
return <div id="dialog-root" /> | ||
}) | ||
|
||
DialogProvider.displayName = 'DialogProvider' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.uadialog { | ||
background-color: black; | ||
height: 100%; | ||
opacity: 0.85; | ||
position: absolute; | ||
width: 100%; | ||
z-index: var(--zindex-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use client' | ||
|
||
import { canUseDOM } from 'exenv' | ||
import type { FC, HTMLAttributes, PropsWithChildren } from 'react' | ||
import { createPortal } from 'react-dom' | ||
|
||
import styles from './dialog.module.css' | ||
|
||
|
||
export type DialogProps = | ||
& PropsWithChildren<{ | ||
id: string | ||
toRender?: boolean | ||
open?: boolean | ||
}> | ||
& HTMLAttributes<HTMLDialogElement> | ||
|
||
export const Dialog: FC<DialogProps> = ({ | ||
id, | ||
toRender, | ||
open, | ||
children, | ||
...props | ||
}) => { | ||
if (!toRender || !canUseDOM) { | ||
return null | ||
} | ||
|
||
const dialogPortal = document.getElementById('dialog-root') | ||
if (!dialogPortal) { | ||
return null | ||
} | ||
|
||
return createPortal( | ||
( | ||
<dialog | ||
className={styles.uadialog} | ||
open={open} | ||
{...props} | ||
> | ||
{children} | ||
</dialog> | ||
), | ||
dialogPortal | ||
) | ||
} | ||
|
||
Dialog.displayName = '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use client' | ||
|
||
import { canUseDOM } from 'exenv' | ||
import { useCallback, useEffect, useMemo, useState, } from 'react' | ||
|
||
import type { DialogProps } from '../dialog' | ||
|
||
|
||
export type UseDialogParams = { | ||
id: string; | ||
open?: boolean; | ||
onOpen?: () => void; | ||
onClose?: () => void; | ||
} | ||
|
||
export type UseDialogHookResult = { | ||
toRender: boolean | ||
dialogProps: DialogProps | ||
openDialog: () => void | ||
closeDialog: () => void | ||
} | ||
|
||
export type UseDialogHook = (options: UseDialogParams) => UseDialogHookResult | ||
|
||
export const useDialog: UseDialogHook = (props) => { | ||
const [toRender, setTodRender] = useState(false) | ||
const [isOpen, setIsOpen] = useState(props?.open ?? false) | ||
|
||
const openDialog = useCallback(() => { | ||
setIsOpen(true) | ||
}, []) | ||
|
||
const closeDialog = useCallback(() => { | ||
setIsOpen(false) | ||
}, []) | ||
|
||
const dialogProps = useMemo(() => { | ||
return { | ||
...props, | ||
open: isOpen, | ||
'aria-labelledby': `${props.id}-title`, | ||
'aria-describedby': `${props.id}-description`, | ||
onOpen(): void { | ||
props.onOpen?.() | ||
}, | ||
onClose(): void { | ||
props.onClose?.() | ||
}, | ||
} | ||
}, [props, isOpen]) | ||
|
||
useEffect(() => { | ||
if (!canUseDOM) { | ||
setTodRender(isOpen) | ||
return | ||
} | ||
|
||
if (isOpen) { | ||
setTodRender(true) | ||
} | ||
}, [isOpen]) | ||
|
||
return { | ||
toRender, | ||
dialogProps, | ||
openDialog, | ||
closeDialog, | ||
} | ||
} |
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 @@ | ||
export * from './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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './search-places-box' |
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,20 @@ | ||
.uasearchplacesbox { | ||
border: 1px solid var(--color-border-primary); | ||
display: flex; | ||
margin-bottom: var(--spacing-element); | ||
margin-left: auto; | ||
padding: var(--spacing-text); | ||
width: max-content; | ||
} | ||
|
||
.uasearchplacesbox__cta { | ||
font-size: smaller; | ||
} | ||
|
||
.uasearchplacesbox__dialog { | ||
align-items: center; | ||
display: flex; | ||
flex-direction: column; | ||
height: 100%; | ||
justify-content: center; | ||
} |
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,46 @@ | ||
'use client' | ||
|
||
import type { FC } from 'react' | ||
import { useCallback } from 'react' | ||
|
||
import styles from './search-places-box.module.css' | ||
|
||
import { Dialog } from 'src/views/dialog' | ||
import { useDialog } from 'src/views/dialog/hooks/useDialog' | ||
|
||
|
||
|
||
export const SearchPlacesBox: FC = () => { | ||
const id = 'search-places' | ||
const title = 'Search places' | ||
const description = 'Filter the list of places' | ||
|
||
const { dialogProps, openDialog, closeDialog, toRender } = useDialog({ id }) | ||
|
||
const onSubmit = useCallback(() => { | ||
closeDialog() | ||
}, [closeDialog]) | ||
|
||
return ( | ||
<div className={styles.uasearchplacesbox}> | ||
<a role="button" className={styles.uasearchplacesbox__cta} onClick={openDialog}>search</a> | ||
|
||
<Dialog {...dialogProps} toRender={toRender}> | ||
<div className={styles.uasearchplacesbox__dialog}> | ||
<button onClick={closeDialog}>close</button> | ||
|
||
<h3 id={dialogProps['aria-labelledby']}>{title}</h3> | ||
<p id={dialogProps['aria-describedby']}>{description}</p> | ||
|
||
<form> | ||
<input name="search-places-name" placeholder="name" /> | ||
|
||
<button onClick={onSubmit}>search</button> | ||
</form> | ||
</div> | ||
</Dialog> | ||
</div> | ||
) | ||
} | ||
|
||
SearchPlacesBox.displayName = 'SearchPlacesBox' |