Skip to content

Commit

Permalink
feat: add a createKey utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ubermanu committed Jun 1, 2023
1 parent 64db343 commit 7784b72
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './interactions/Press/press.js'
export * from './interactions/TypeAhead/typeAhead.js'
export * from './stores/activeElement.js'
export * from './stores/interactionMode.js'
export * from './utilities/key.js'
7 changes: 7 additions & 0 deletions src/lib/utilities/key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Generates a random key for the components that need one. */
export const createKey = () => {
return (
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts">
import type { Accordion } from '$lib'
import { createKey, type Accordion } from '$lib'
import { getContext } from 'svelte'
export let heading: string
export let disabled: boolean = false
export let open: boolean = false
// Generate a random key for this accordion item
const key = Math.random().toString(36).substring(7)
const key = createKey()
const { triggerAttrs, contentAttrs, expanded, expand, disable } =
getContext<Accordion>('accordion')
Expand Down
10 changes: 8 additions & 2 deletions src/routes/(docs)/component/menu/example/MenuItem.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<script lang="ts">
import { createPopover, useHover, useFocusWithin, type Menu } from '$lib'
import {
createKey,
createPopover,
useHover,
useFocusWithin,
type Menu,
} from '$lib'
import { getContext, setContext, tick } from 'svelte'
import { ChevronRight } from 'lucide-svelte'
export let href: string
// Generate a random key for this menu item
const key = Math.random().toString(36).substring(7)
const key = createKey()
// Check if this menu item has a submenu
const hasSubmenu = $$slots.submenu !== undefined
Expand Down
7 changes: 3 additions & 4 deletions src/routes/(docs)/component/toolbar/example/Button.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<script lang="ts">
import type { Toolbar } from '$lib'
import { createKey, type Toolbar } from '$lib'
import { getContext } from 'svelte'
// Generate a unique key for this item
const key = Math.random().toString(36).substr(2, 9)
const key = createKey()
const { itemAttrs } = getContext<Toolbar>('toolbar')
</script>

<button
{...$itemAttrs(key)}
class="rounded p-2 hover:bg-neutral-200 dark:hover:bg-neutral-800
"
class="rounded p-2 hover:bg-neutral-200 dark:hover:bg-neutral-800"
on:click
{...$$restProps}
>
Expand Down

0 comments on commit 7784b72

Please sign in to comment.