Skip to content

Commit

Permalink
fix: Tabs overflow handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ieedan committed Aug 13, 2024
1 parent 96a0def commit 6ffd896
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/shared/demo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</p>
{/if}
<div
class="group relative mt-4 w-full overflow-x-auto rounded-xl border border-gray-alpha-400 bg-background-100 md:overflow-visible xl:mt-7"
class="group relative mt-4 overflow-x-auto rounded-xl border border-gray-alpha-400 bg-background-100 xl:mt-7"
>
<section id="component-demo" class={cn('w-full p-6', class_name)}>
<slot></slot>
Expand Down
43 changes: 41 additions & 2 deletions src/lib/components/ui/tabs/tabs-list.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { cn } from '$lib/utils.js';
import { Tabs as TabsPrimitive } from 'bits-ui';
import { onMount } from 'svelte';
type $$Props = TabsPrimitive.ListProps & {
variant?: 'default' | 'secondary';
Expand All @@ -9,16 +10,54 @@
let class_name: $$Props['class'] = undefined;
export let variant: $$Props['variant'] = 'default';
export { class_name as class };
let el: HTMLDivElement;
let preBlur = false;
let postBlur = false;
// When the tabs are scrolled it will determine whether or not to show the blur at the end
const scroll = () => {
if (el.offsetWidth + el.scrollLeft < el.scrollWidth) {
postBlur = true;
} else {
postBlur = false;
}
if (el.scrollLeft == 0) {
preBlur = false;
} else {
preBlur = true;
}
};
onMount(() => {
scroll(); // initially determine blur state
el.addEventListener('scroll', scroll); // on:scroll listener doesn't work on TabsPrimitive
return () => {
el.removeEventListener('scroll', scroll);
};
});
</script>

<!-- TODO: Add a no-scrollbar class. -->
<!-- Make sure you have the `no-scrollbar` class in your tailwind.config file -->

<TabsPrimitive.List
data-variant={variant}
bind:el
class={cn(
'group inline-flex w-full flex-wrap items-baseline gap-3 overflow-hidden overflow-x-auto p-1 text-gray-900 data-[variant=default]:[box-shadow:_0_-1px_0_var(--accents-2)_inset;]',
'group flex w-full items-baseline gap-3 overflow-hidden overflow-x-auto p-1 text-gray-900 no-scrollbar data-[variant=default]:[box-shadow:_0_-1px_0_var(--accents-2)_inset;]',
class_name
)}
{...$$restProps}
>
{#if preBlur}
<span class="absolute left-4 h-10 w-8 bg-gray-100 bg-opacity-80 blur-lg"></span>
{/if}
<slot />
<!-- This indicates to the user that there is more content -->
{#if postBlur}
<span class="absolute right-4 h-10 w-8 bg-gray-100 bg-opacity-80 blur-lg"></span>
{/if}
</TabsPrimitive.List>
6 changes: 6 additions & 0 deletions src/routes/tabs/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import default_code from './default.svelte?raw';
import DisableSpecificTabs from './disable-specific-tabs.svelte';
import disable_specific_tabs_code from './disable-specific-tabs.svelte?raw';
import Overflow from './overflow-tabs.svelte';
import overflow_code from './overflow-tabs.svelte?raw';
import Secondary from './secondary.svelte';
import secondary_code from './secondary.svelte?raw';
import WithIcons from './with-icons.svelte';
Expand All @@ -29,4 +31,8 @@
<Demo id="secondary" code={secondary_code}>
<Secondary />
</Demo>

<Demo id="overflow" code={overflow_code}>
<Overflow />
</Demo>
</PageWrapper>
29 changes: 29 additions & 0 deletions src/routes/tabs/overflow-tabs.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script lang="ts">
import * as Tabs from '$lib/components/ui/tabs';
const tabs = [
{ title: 'Apple', value: 'apple' },
{ title: 'Banana', value: 'banana' },
{ title: 'Orange', value: 'orange' },
{ title: 'Strawberry', value: 'strawberry' },
{ title: 'Mango', value: 'mango' },
{ title: 'Pineapple', value: 'pineapple' },
{ title: 'Grape', value: 'grape' },
{ title: 'Watermelon', value: 'watermelon' },
{ title: 'Peach', value: 'peach' },
{ title: 'Kiwi', value: 'kiwi' },
{ title: 'Cherry', value: 'cherry' },
{ title: 'Blueberry', value: 'blueberry' },
{ title: 'Pear', value: 'pear' },
{ title: 'Plum', value: 'plum' },
{ title: 'Raspberry', value: 'raspberry' }
];
</script>

<Tabs.Root value="apple">
<Tabs.List>
{#each tabs as { title, value }}
<Tabs.Trigger {value}>{title}</Tabs.Trigger>
{/each}
</Tabs.List>
</Tabs.Root>
16 changes: 15 additions & 1 deletion tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,21 @@ const config: Config = {
}
}
},
plugins: [typography]
plugins: [
typography,
function ({ addUtilities }) {
const newUtilities = {
'.no-scrollbar': {
'-ms-overflow-style': 'none',
'scrollbar-width': 'none',
'&::-webkit-scrollbar': {
display: 'none'
}
}
};
addUtilities(newUtilities);
}
]
};

export default config;

0 comments on commit 6ffd896

Please sign in to comment.