Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger prop #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ import { commandScore } from './command-score'
type Children = { children?: React.ReactNode }
type DivProps = React.HTMLAttributes<HTMLDivElement>

type LoadingProps = Children & DivProps & {
/** Estimated progress of loading asynchronous options. */
progress?: number
}
type LoadingProps = Children &
DivProps & {
/** Estimated progress of loading asynchronous options. */
progress?: number
}
type EmptyProps = Children & DivProps & {}
type SeparatorProps = DivProps & {
/** Whether this separator should always be rendered. Useful if you disable automatic filtering. */
alwaysRender?: boolean
}
type DialogProps = RadixDialog.DialogProps &
CommandProps & {
/** Provide a className to the Dialog overlay. */
overlayClassName?: string
/** Provide a className to the Dialog content. */
contentClassName?: string
/** Provide a custom element the Dialog should portal into. */
container?: HTMLElement
}
type CommonDialogProps = {
/** Provide a className to the Dialog overlay. */
overlayClassName?: string
/** Provide a className to the Dialog content. */
contentClassName?: string
/** Provide a custom element the Dialog should portal into. */
container?: HTMLElement
}
type DialogProps = RadixDialog.DialogProps & CommandProps & CommonDialogProps
type DialogPortalWrapperProps = RadixDialog.DialogPortalProps & CommandProps & CommonDialogProps
type DialogPortalProps = {
ref: React.ForwardedRef<HTMLDivElement>
} & (DialogProps | DialogPortalWrapperProps)
type ListProps = Children & DivProps & {}
type ItemProps = Children &
Omit<DivProps, 'disabled' | 'onSelect' | 'value'> & {
Expand Down Expand Up @@ -799,23 +804,37 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, forwardedRef) =
)
})

const DialogPortal = ({ overlayClassName, contentClassName, container, label, ref, ...etc }: DialogPortalProps) => {
return (
<RadixDialog.Portal container={container}>
<RadixDialog.Overlay cmdk-overlay="" className={overlayClassName} />
<RadixDialog.Content aria-label={label} cmdk-dialog="" className={contentClassName}>
<Command ref={ref} {...etc} />
</RadixDialog.Content>
</RadixDialog.Portal>
)
}

/**
* Renders the command menu in a Radix Dialog.
*/
const Dialog = React.forwardRef<HTMLDivElement, DialogProps>((props, forwardedRef) => {
const { open, onOpenChange, overlayClassName, contentClassName, container, ...etc } = props
return (
<RadixDialog.Root open={open} onOpenChange={onOpenChange}>
<RadixDialog.Portal container={container}>
<RadixDialog.Overlay cmdk-overlay="" className={overlayClassName} />
<RadixDialog.Content aria-label={props.label} cmdk-dialog="" className={contentClassName}>
<Command ref={forwardedRef} {...etc} />
</RadixDialog.Content>
</RadixDialog.Portal>
<DialogPortal ref={forwardedRef} {...etc} />
</RadixDialog.Root>
)
})

/**
* Renders the command menu in a Radix Dialog without the root element
*/
const DialogPortalWrapper = React.forwardRef<HTMLDivElement, DialogPortalWrapperProps>((props, forwardedRef) => {
const { overlayClassName, contentClassName, container, ...etc } = props
return <DialogPortal ref={forwardedRef} {...etc} />
})

/**
* Automatically renders when there are no results for the search query.
*/
Expand Down Expand Up @@ -860,6 +879,7 @@ const pkg = Object.assign(Command, {
Group,
Separator,
Dialog,
DialogPortal: DialogPortalWrapper,
Empty,
Loading,
})
Expand Down