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

Feat: expose complete stateStore as a slot prop #38

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/wicked-cars-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cmdk-sv': patch
---

feat: expose state slot prop for Command.Root
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ You can make the arrow keys wrap around the list (when you reach the end, it goe
<Command.Root loop />
```

This component also exposes two additional slot props for `state` (the current reactive value of the command state) and `stateStore` (the underlying writable state store). These can be used to implement more advanced use cases, such as debouncing the search updates with the `stateStore.updateState` method:

```svelte
<Command.Root {state} let:stateStore>
{@const handleUpdateState = debounce(stateStore.updateState, 200)}
<CustomCommandInput {handleUpdateState} />
</Command.Root>
```

Comment on lines +122 to +130
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to keep it relatively brief, as I don't imagine it will be super commonly used. Also writing this very late at night so please proof-read and edit as you please 😅

### Dialog `[cmdk-dialog]` `[cmdk-overlay]`

Props are forwarded to [Command](#command-cmdk-root). Composes Bits UI's Dialog component. The overlay is always rendered. See the [Bits Documentation](https://bits-ui.com/docs/) for more information. Can be controlled by binding to the `open` prop.
Expand Down
11 changes: 9 additions & 2 deletions src/lib/cmdk/components/Command.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,23 @@
action: rootAction,
attrs: rootAttrs
};

$: slotProps = {
root,
label: { attrs: labelAttrs },
stateStore,
state: $stateStore
};
</script>

{#if asChild}
<slot {root} label={{ attrs: labelAttrs }} />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move these into a reactive slotProps prop, so something like:

<script lang="ts">
	$: slotProps = {
		root,
		label: { attrs: labelAttrs },
		stateStore,
		state: $stateStore,
	}
</script>

If we're going to pass stores as slot props I think they should be suffixed with Store and then we can also pass the value of the store as the state prop.

<slot {...slotProps} />
{:else}
<div use:rootAction {...rootAttrs} {...$$restProps}>
<!-- svelte-ignore a11y-label-has-associated-control applied in attrs -->
<label {...labelAttrs}>
{label ?? ''}
</label>
<slot {root} label={{ attrs: labelAttrs }} />
<slot {...slotProps} />
</div>
{/if}
Loading