-
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.
refactor(components): extract modal component (#619)
- Loading branch information
1 parent
5aa4802
commit 95358f9
Showing
5 changed files
with
113 additions
and
66 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
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,44 @@ | ||
import { type Meta, type StoryObj } from '@storybook/preact'; | ||
import { expect, waitFor, within } from '@storybook/test'; | ||
import { type FunctionComponent } from 'preact'; | ||
|
||
import { Modal, type ModalProps, useModalRef } from './modal'; | ||
|
||
const meta: Meta<ModalProps> = { | ||
title: 'Component/Modal', | ||
component: Modal, | ||
parameters: { fetchMock: {} }, | ||
}; | ||
|
||
export default meta; | ||
|
||
const WrapperWithButtonThatOpensTheModal: FunctionComponent = () => { | ||
const modalRef = useModalRef(); | ||
|
||
return ( | ||
<div> | ||
<button className='btn' onClick={() => modalRef.current?.showModal()}> | ||
Open modal | ||
</button> | ||
<Modal modalRef={modalRef}> | ||
<h1>Modal content</h1> | ||
</Modal> | ||
</div> | ||
); | ||
}; | ||
|
||
export const ModalStory: StoryObj<ModalProps> = { | ||
render: () => { | ||
return <WrapperWithButtonThatOpensTheModal />; | ||
}, | ||
play: async ({ canvasElement, step }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await step('Open the modal', async () => { | ||
const button = canvas.getByText('Open modal'); | ||
button.click(); | ||
|
||
await waitFor(() => expect(canvas.getByText('Modal content')).toBeVisible()); | ||
}); | ||
}, | ||
}; |
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,31 @@ | ||
import { type FunctionComponent, type Ref } from 'preact'; | ||
import { useRef } from 'preact/hooks'; | ||
|
||
export type ModalProps = { | ||
modalRef: Ref<HTMLDialogElement>; | ||
}; | ||
|
||
export function useModalRef() { | ||
return useRef<HTMLDialogElement>(null); | ||
} | ||
|
||
export const Modal: FunctionComponent<ModalProps> = ({ children, modalRef }) => { | ||
return ( | ||
<dialog ref={modalRef} className={'modal modal-bottom sm:modal-middle'}> | ||
<div className='modal-box sm:max-w-5xl'> | ||
<form method='dialog'> | ||
<button className='btn btn-sm btn-circle btn-ghost absolute right-2 top-2'>✕</button> | ||
</form> | ||
<div className={'flex flex-col'}>{children}</div> | ||
<div className='modal-action'> | ||
<form method='dialog'> | ||
<button className={'float-right underline text-sm hover:text-blue-700 mr-2'}>Close</button> | ||
</form> | ||
</div> | ||
</div> | ||
<form method='dialog' className='modal-backdrop'> | ||
<button>Helper to close when clicked outside</button> | ||
</form> | ||
</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