Skip to content

Commit

Permalink
Make tags more customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Sep 6, 2024
1 parent a1d276b commit 245809f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.26.6",
"version": "0.26.7",
"repository": "github:ShaitanLyss/selenite-commons",
"license": "MIT",
"keywords": [
Expand Down
31 changes: 25 additions & 6 deletions src/lib/components/Tags.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@
knownTags?: string[];
popupBg?: string;
tagProps?: HTMLButtonAttributes;
tagBadge?: string;
addTagProps?: HTMLButtonAttributes;
}
let { tags = $bindable([]), knownTags = [], popupBg = 'bg-base-200', tagProps = {}, ...props }: Props = $props();
let {
tags = $bindable([]),
knownTags = [],
popupBg = 'bg-base-200',
tagBadge = 'badge-secondary',
tagProps = {},
addTagProps = {},
...props
}: Props = $props();
const tagsSet = $derived(new Set(tags));
let creatingTag = $state(false);
Expand All @@ -41,7 +51,7 @@
</script>

{#snippet Tag(label: string, props: HTMLButtonAttributes = {})}
<button type="button" {...props} {...tagProps} class="badge my-2 {props.class} {tagProps.class}">
<button type="button" {...props} class="badge my-2 {props.class} {tagProps.class}">
{label}
</button>
{/snippet}
Expand All @@ -65,7 +75,14 @@
<ul {...props} class="flex flex-wrap gap-2 items-center {props.class}">
{#each tags as tag, i (tag)}
<li animate:flip={{ duration: 200 }}>
{@render Tag(tag, { class: 'hover:badge-error', onclick: () => tags.splice(i, 1) })}
{@render Tag(tag, {
...tagProps,
class: `hover:badge-error ${tagBadge} ${tagProps.class}`,
onclick: (e) => {
tagProps.onclick?.(e);
tags.splice(i, 1);
}
})}
</li>
{/each}
{#if creatingTag}
Expand All @@ -89,7 +106,7 @@
}}
use:keys={{
enter: (e) => {
e.preventDefault()
e.preventDefault();
if (!e.target.value) {
addTag(filteredKnownTags[0]);
return;
Expand All @@ -107,12 +124,14 @@
{/if}
<li>
{@render Tag('+ Add tag', {
class: 'hover:badge-accent',
onclick: () => {
...addTagProps,
class: `hover:badge-accent ${addTagProps.class}`,
onclick: (e) => {
if (creatingTag) {
addNewTag();
}
creatingTag = true;
addTagProps.onclick?.(e);
}
})}
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/tags/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<p>
The <code>Tags</code> component.
</p>
<Tags bind:tags {knownTags} class="w-[30rem]"/>
<Tags bind:tags {knownTags} class="w-[30rem]" />
Number of tags : {tags.length}
</div>

0 comments on commit 245809f

Please sign in to comment.