Skip to content

Commit

Permalink
feat: #1054 #933 #1440 add use:action to SidebarItem and A component
Browse files Browse the repository at this point in the history
  • Loading branch information
shinokada committed Sep 26, 2024
1 parent b3fad6f commit f0453e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/lib/sidebar/SidebarItem.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<script lang="ts">
import type { Action } from 'svelte/action';
import { getContext } from 'svelte';
import { twMerge } from 'tailwind-merge';
import type { SidebarType } from './Sidebar.svelte';
export let action: Action<HTMLElement, any> = () => {};
export let params: any = {};
export let href: string = '';
export let label: string = '';
export let spanClass: string = 'ms-3';
Expand All @@ -27,7 +30,7 @@
</script>

<li>
<a {...$$restProps} {href} on:blur on:click on:focus on:keydown on:keypress on:keyup on:mouseenter on:mouseleave on:mouseover class={aClass}>
<a {...$$restProps} {href} use:action={params} on:blur on:click on:focus on:keydown on:keypress on:keyup on:mouseenter on:mouseleave on:mouseover class={aClass}>
<slot name="icon" />
<span class={spanClass}>{label}</span>
{#if $$slots.subtext}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/typography/A.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<script lang="ts">
import type { Action } from 'svelte/action';
import { twMerge } from 'tailwind-merge';
export let action: Action<HTMLElement, any> = () => {};
export let params: any = {};
export let href: string = '#';
export let color: string = 'text-primary-600 dark:text-primary-500';
export let aClass: string = 'inline-flex items-center hover:underline';
</script>

<a {...$$restProps} {href} class={twMerge(aClass, color, $$props.class)} on:click>
<a {...$$restProps} {href} use:action={params} class={twMerge(aClass, color, $$props.class)} on:click>
<slot />
</a>

Expand Down
44 changes: 44 additions & 0 deletions src/routes/docs/components/sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,50 @@ You can add own transition by setting `transitionType` and `transitionParams`.
</Sidebar>
```

## use:action

Svelte Actions are essentially element-level lifecycle functions. They're useful for things like:

- interfacing with third-party libraries
- lazy-loaded images
- tooltips
- adding custom event handlers

The `SidebarItem` component has `use:action` directive you can use:

```svelte example
<script>
import { Sidebar, SidebarGroup, SidebarItem, SidebarWrapper } from 'flowbite-svelte';
const routes = [
{
label: 'Home',
href: '/'
},
{
label: 'Contact',
href: '/contact'
},
{
label: 'Profile',
href: '/profile'
}
];
const myaction = (label) => {
console.log('Hello ', label);
};
</script>
<Sidebar >
<SidebarWrapper>
<SidebarGroup>
{#each routes as {label, href}}
<SidebarItem {label} {href} action={myaction(label)}/>
{/each}
</SidebarGroup>
</SidebarWrapper>
</Sidebar>
```

## Component data

The component has the following props, type, and default values. See [types page](/docs/pages/typescript) for type information.
Expand Down
15 changes: 15 additions & 0 deletions src/routes/docs/typography/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ This example can be used to set a hyperlink on an image inside a card component.
</Card>
```

## use:action

```svelte example
<script>
import { A } from 'flowbite-svelte';
const myaction = () => {
console.log('Action triggered');
};
</script>
<A href="/" action={myaction}>Read more</A>
```

## Component data

The component has the following props, type, and default values. See [types page](/docs/pages/typescript) for type information.
Expand Down

0 comments on commit f0453e0

Please sign in to comment.