Skip to content

Commit 4f0c60a

Browse files
committed
feat: cancel tab click if event.defaultPrevented
This lets user cancel the tab change. For example, if you show a confirm asking "Are you sure you want to switch tabs without saving?" and they press cancel.
1 parent ca21255 commit 4f0c60a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/tab/src/Tab.svelte

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
aria-selected={active ? 'true' : 'false'}
3232
tabindex={active || forceAccessible ? '0' : '-1'}
3333
{href}
34-
on:click={() => instance && instance.handleClick()}
34+
on:click={handleClick}
3535
{...internalAttrs}
3636
{...exclude($$restProps, ['content$', 'tabIndicator$'])}
3737
>
@@ -66,24 +66,25 @@
6666
</svelte:component>
6767

6868
<script lang="ts">
69-
import type { TabIndicatorComponentDev } from '@smui/tab-indicator';
7069
import { MDCTabFoundation } from '@material/tab';
7170
import { onMount, setContext, getContext } from 'svelte';
72-
import { get_current_component, SvelteComponentDev } from 'svelte/internal';
71+
import type { SvelteComponentDev } from 'svelte/internal';
72+
import { get_current_component } from 'svelte/internal';
73+
import type { ActionArray } from '@smui/common/internal';
7374
import {
7475
forwardEventsBuilder,
7576
classMap,
7677
exclude,
7778
prefixFilter,
7879
useActions,
7980
dispatch,
80-
ActionArray,
8181
} from '@smui/common/internal';
8282
import Ripple from '@smui/ripple';
8383
import { A, Button } from '@smui/common/elements';
84+
import type { TabIndicatorComponentDev } from '@smui/tab-indicator';
8485
import TabIndicator from '@smui/tab-indicator';
8586
86-
import type { SMUITabAccessor } from './Tab.types.js';
87+
import type { SMUITabAccessor } from './Tab.types';
8788
8889
const forwardEvents = forwardEventsBuilder(get_current_component());
8990
@@ -181,6 +182,12 @@
181182
};
182183
});
183184
185+
function handleClick(event: PointerEvent) {
186+
if (!event.defaultPrevented) {
187+
instance?.handleClick();
188+
}
189+
}
190+
184191
function hasClass(className: string) {
185192
return className in internalClasses
186193
? internalClasses[className]

0 commit comments

Comments
 (0)