Skip to content

Commit

Permalink
fix(dialog): Dialog.Content children type unassignable (#13)
Browse files Browse the repository at this point in the history
* fix(dialog): Dialog.Content children type unassignable

* feat(dialog): add dialog stories
  • Loading branch information
PHILLIPS71 authored Apr 15, 2024
1 parent c786e56 commit 44a390d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
39 changes: 39 additions & 0 deletions packages/react/src/components/dialog/Dialog.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { DialogProps } from '@/components/dialog'
import type { Meta, StoryFn } from '@storybook/react'

import { Button } from '@/components/button'
import { Card } from '@/components/card'
import { Dialog } from '@/components/dialog'

const Component: Meta<typeof Dialog> = {
title: 'Components/Dialog',
component: Dialog,
}

const defaultProps = {}

export const Default: StoryFn = (args: DialogProps) => (
<Dialog {...args}>
<Button>Open</Button>

<Dialog.Content>
{({ close }) => (
<Card>
<Card.Header>Dialog</Card.Header>

<Card.Body>Dialog Content</Card.Body>

<Card.Footer>
<Button onPress={close}>Close</Button>
</Card.Footer>
</Card>
)}
</Dialog.Content>
</Dialog>
)

Default.args = {
...defaultProps,
}

export default Component
17 changes: 9 additions & 8 deletions packages/react/src/components/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type { UseDialogProps } from '@/components/dialog/use-dialog.hook'
import type { ComponentWithoutAs } from '@/utilities/types'
import type { DialogTriggerProps as ComponentProps } from 'react-aria-components'
import type { DialogTriggerProps } from 'react-aria-components'

import React from 'react'
import { DialogTrigger as Component } from 'react-aria-components'
import { DialogTrigger } from 'react-aria-components'

import DialogContent from '@/components/dialog/DialogContent'
import { DialogContext, useDialog } from '@/components/dialog/use-dialog.hook'

export type DialogProps = ComponentWithoutAs<'div'> & ComponentProps & UseDialogProps
type ComponentProps = ComponentWithoutAs<'div'> & Omit<DialogTriggerProps, 'children'> & UseDialogProps

const Dialog = React.forwardRef<HTMLDivElement, DialogProps>((props, ref) => {
const Component = React.forwardRef<HTMLDivElement, ComponentProps>((props, ref) => {
const { children, className, blur, placement, ...rest } = props

const context = useDialog({ blur, placement })

const component = React.useMemo<ComponentProps>(
const trigger = React.useMemo<DialogTriggerProps>(
() => ({
ref,
children,
Expand All @@ -27,13 +27,14 @@ const Dialog = React.forwardRef<HTMLDivElement, DialogProps>((props, ref) => {

return (
<DialogContext.Provider value={context}>
<Component {...component}>{children}</Component>
<DialogTrigger {...trigger}>{children}</DialogTrigger>
</DialogContext.Provider>
)
})

Dialog.displayName = 'Dialog'
Component.displayName = 'Dialog'

export default Object.assign(Dialog, {
export { ComponentProps as DialogProps }
export default Object.assign(Component, {
Content: DialogContent,
})
10 changes: 5 additions & 5 deletions packages/react/src/components/dialog/DialogContent.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { ComponentWithoutAs } from '@/utilities/types'
import type { DialogProps, ModalOverlayProps } from 'react-aria-components'

import React from 'react'
import { Dialog, Modal, ModalOverlay } from 'react-aria-components'

import { useDialogContext } from '@/components/dialog/use-dialog.hook'

export type DialogContentProps = ComponentWithoutAs<'div'> & DialogProps
type ComponentProps = DialogProps

const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps>((props, ref) => {
const Component = React.forwardRef<HTMLDivElement, ComponentProps>((props, ref) => {
const { children, className, ...rest } = props

const { slots } = useDialogContext()
Expand Down Expand Up @@ -45,6 +44,7 @@ const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps>((prop
)
})

DialogContent.displayName = 'Dialog.Content'
Component.displayName = 'Dialog.Content'

export default DialogContent
export { ComponentProps as DialogContentProps }
export default Component

0 comments on commit 44a390d

Please sign in to comment.