Use this to detect container overflow and show/hide scroll controls. Note: The overflowFade action also emits these same events while adding visual fades.
<script>
import { isOverflowingAction } from 'svelte-overflow-fade';
let showLeftBtn = $state(false);
let showRightBtn = $state(false);
</script>
<div
class="relative"
>
<div
class="overflow-auto"
use:isOverflowingAction={{
axis: 'x'
}}
onoverflow={({ detail }) => {
showLeftBtn = detail.overflowLeft;
showRightBtn = detail.overflowRight;
}}
>
<!-- Scrollable content -->
</div>
{#if showLeftBtn}
<button class="absolute left-4">←</button>
{/if}
{#if showRightBtn}
<button class="absolute right-4">→</button>
{/if}
</div>Compatible with all Svelte versions
<script>
import { overflowFadeAction } from 'svelte-overflow-fade';
</script>
<div
class="overflow-auto max-h-64"
use:overflowFadeAction={{
axis: 'y',
fade: {
type: 'mask',
fadePercent: 10
}
}}
onoverflow={({ detail }) => {
// Also emits overflow events!
console.log(detail.overflowTop, detail.overflowBottom);
}}
>
<!-- Scrollable content -->
</div>Svelte 5.29+ with enhanced reactivity
<script>
import { overflowFade } from 'svelte-overflow-fade';
</script>
<div
class="overflow-auto max-h-64"
{@attach overflowFade({
axis: 'y',
fade: {
type: 'mask',
fadePercent: 10
}
})}
>
<!-- Scrollable content -->
</div>To avoid TypeScript errors with the onoverflow event, add this to your app.d.ts or a separate .d.ts file:
/// <reference types="svelte" />
import type { OverflowState } from 'svelte-overflow-fade';
declare global {
namespace svelteHTML {
interface HTMLAttributes<T> {
// Svelte 5 syntax
'onoverflow'?: (e: CustomEvent<OverflowState>) => void;
// Svelte 4 syntax (if needed)
'on:overflow'?: (e: CustomEvent<OverflowState>) => void;
}
}
}
export {};Note: The declare global wrapper is essential for the types to work correctly.