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

Why is Command.Empty disabled on first render? #149

Closed
CapitaineToinon opened this issue Jun 25, 2023 · 4 comments
Closed

Why is Command.Empty disabled on first render? #149

CapitaineToinon opened this issue Jun 25, 2023 · 4 comments

Comments

@CapitaineToinon
Copy link

CapitaineToinon commented Jun 25, 2023

After spending a good 30 minutes trying to debug why I couldn't get the Empty element to show up asap when the command opens, I ended up searching to code to find that it is explicitly disabled on first render.

https://github.com/pacocoursey/cmdk/blob/main/cmdk/src/index.tsx#L822-L832

I didn't find any documentation about this and this doesn't seems to be configurable. I would like to have the empty warning show up asap, how can I achieve this? Why is this disabled on first render?

@CapitaineToinon CapitaineToinon changed the title Command.Empty disabled on first render Why is Command.Empty disabled on first render? Jun 25, 2023
@CapitaineToinon
Copy link
Author

CapitaineToinon commented Jun 25, 2023

I was using cmdk with shadcn/ui, I just copied the Empty implementation and removed the ref logic

const CommandEmpty = React.forwardRef<
	HTMLDivElement,
	React.ComponentProps<typeof CommandPrimitive.Empty>
>(({ className, ...props }, forwardedRef) => {
	const render = useCommandState((state) => state.filtered.count === 0)

	if (!render) return null

	return (
		<div
			ref={forwardedRef}
			className={cn('py-6 text-center text-sm', className)}
			cmdk-empty=""
			role="presentation"
			{...props}
		/>
	)
})

@donfour
Copy link

donfour commented Aug 15, 2023

following, also interested

@cdslxc
Copy link

cdslxc commented Sep 18, 2023

I need this feature.

osovv added a commit to osovv/big-table-example that referenced this issue Oct 29, 2023
@soags
Copy link

soags commented Dec 1, 2023

The recommended code in the README under "Use inside Popover" does not work with Empty.
https://github.com/pacocoursey/cmdk#use-inside-popover

import * as Popover from '@radix-ui/react-popover'

return (
  <Popover.Root>
    <Popover.Trigger>Toggle popover</Popover.Trigger>    
    <Popover.Content>
      <Command>
        <Command.Input />
        <Command.Empty>Empty</Command.Empty>
        <Command.List></Command.List>
      </Command>
    </Popover.Content>
  </Popover.Root>
)

What is the reason for needing to skip the first render?
If there is a reason, it would be nice to be able to change the behavior with an option.

import * as Popover from '@radix-ui/react-popover'

return (
  <Popover.Root>
    <Popover.Trigger>Toggle popover</Popover.Trigger>    
    <Popover.Content>
      <Command>
        <Command.Input />
        <Command.Empty firstRender={true}>Empty</Command.Empty>
        <Command.List></Command.List>
      </Command>
    </Popover.Content>
  </Popover.Root>
)

type EmptyProps = Children & DivProps & {
  firstRender?: boolean
}

const Empty = React.forwardRef<HTMLDivElement, EmptyProps>(({ firstRender= false, ...props}, forwardedRef) => {
  const isFirstRender = React.useRef(true)
  const render = useCmdk((state) => state.filtered.count === 0)

  React.useEffect(() => {
    isFirstRender.current = false
  }, [])

  if ((!firstRender && isFirstRender.current) || !render) return null
  return <div ref={forwardedRef} {...props} cmdk-empty="" role="presentation" />
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants