Some stuff I often use...
stores
<script>
import { Device } from 'fractils'
</script>
<Device />
<script>
import { mobileThreshold, mobile, screenH, screenW, scrollY } from 'fractils'
$mobileThreshold = 1000
</script>
mobile: {$mobile}
screenH: {$screenH}
screenW: {$screenW}
scrollY: {$scrollY}
mouse: {$mouse.x}, {$mouse.y}
store
<script>
import { localStorageStore } from 'fractils'
const count = localStorageStore('count', 0)
</script>
<div on:click={() => $count++}>+</div>
{$count}
<div on:click={() => $count--}>-</div>
function
A simple logger that only runs in dev environments.
<script>
import { log } from 'fractils'
log('Hello world', 'blue', '#222', 20, 'font-weight: bold;')
</script>
Hello world
β Β
Add log.ts
to devtools
ignore list
to forward stack traces
(It's pretty annoying when you click on a log's link in devtools and it takes you
to the logger file instead of the callsite).
function
<script>
import { wait } from 'fractils'
let ready, set, go
async function start() {
ready = true
await wait(500)
set = true
await wait(500)
go = true
}
</script>
ready
set
go
function
<script>
import { mapRange } from 'fractils'
let value = 50
$: valueMapped = mapRange(value, 0, 100, -10, 10)
</script>
{value}
<input bind:value type='range'/>
{valueMapped}
function
<script>
import { clamp } from 'fractils'
$: value = clamp(value ?? 50, 25, 75)
</script>
{value}
<input bind:value type='range'/>
component
<script>
import { ThemeToggle } from 'fractils'
</script>
<ThemeToggle />
store + functions
<script>
import { theme, initTheme } from 'fractils'
import { onMount } from 'svelte'
onMount(() => initTheme())
</script>
{$theme}
<script>
import { toggleTheme, applyTheme } from 'fractils'
</script>
<button on:click={() => applyTheme("light")}> Light </button>
<button on:click={toggleTheme}> Toggle </button>
<button on:click={() => applyTheme("dark")}> Dark </button>
action
<script lang="ts">
import { clickOutside } from 'fractils'
let clickedOutside
function handleClickOutside(e: CustomEvent) {
clickedOutside = true
console.log(e.detail.target) // the element that was clicked
}
</script>
<div
use:clickOutside="{{ whitelist: ['notme'] }}"
on:outclick="{handleClickOutside}"
>
clickedOutside = {clickedOutside}
</div>
<div class="notme">π«</div>
action
<script>
import { bounceOut } from 'svelte/easing'
import { fly } from 'svelte/transition'
import { visibility } from 'fractils'
let visible, scrollDir, options = { threshold: 0.75 }
function handleChange(e) {
visible = e.detail.isVisible
scrollDir = e.detail.scrollDirection
}
</script>
<div use:visibility={options} on:v-change={handleChange}>
{#if visible}
<div in:fly={{ y: -20, easing: bounceOut }}>
going {scrollDir === 'down' ? 'β¬' : 'β¬'}
</div>
{/if}
</div>
component
<script>
import { Tooltip } from 'fractils'
</script>
<Tooltip content="Thanks!">
<div class="hover-1"> Hover over me! </div>
</Tooltip>
component
<script>
import { OnMount } from 'fractils'
</script>
<OnMount>
<div in:fly={{ x: 100, duration: 1000 }}>
My intro transition will always play!
</div>
</OnMount>
component
Replaces the default scrollbar with a MacOS-style scrolbar.
You probably shouldn't use this for anything that needs to be accessible (most things).<script>
import { MacScrollbar } from 'fractils'
</script>
<MacScrollbar />