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>