Idle: {idle.current}
Last active: {new Date(idle.lastActive).toLocaleTimeString()}
``` -------------------------------- ### SvelteKit Date Input Example (Svelte) Source: https://runed.dev/docs/utilities/use-search-params Demonstrates a practical Svelte component using `useSearchParams` and `createSearchParamsSchema` to manage date inputs. It binds two date parameters, `eventDate` (formatted as 'date') and `createdAt` (formatted as 'datetime'), to corresponding HTML input elements, updating the URL dynamically. ```svelte ``` -------------------------------- ### IsInViewport Usage and Type Definition Source: https://runed.dev/docs/utilities/is-in-viewport Demonstrates how to use the IsInViewport class to track element visibility in the viewport and shows the TypeScript type definitions. The usage example shows importing the class, creating an instance with a reactive state element, and accessing the current visibility state. The type definition shows the constructor parameters and current property access. ```typescriptTarget node
Target node in viewport: {inViewport.current}
``` ```typescript import { type UseIntersectionObserverOptions } from "runed"; export type IsInViewportOptions = UseIntersectionObserverOptions; export declare class IsInViewport { constructor(node: MaybeGetterCount: {count.current}
Counter: {interval.counter}
Interval delay: {delay}ms
Status: {interval.isActive ? "Running" : "Paused"}
``` -------------------------------- ### Resource with Multiple Dependencies Source: https://runed.dev/docs/utilities/resource Shows how to configure the `resource` utility to watch multiple reactive dependencies. The fetcher function receives an array of the current values of these dependencies, allowing for more complex data fetching scenarios. ```typescript const results = resource([() => query, () => page], async ([query, page]) => { const res = await fetch(`/api/search?q=${query}&page=${page}`); return res.json(); }); ``` -------------------------------- ### Creating StateHistory Instance Source: https://runed.dev/docs/utilities/state-history Initializes StateHistory with a getter to track state and a setter to update it. Requires the 'runed' import and a reactive state like $state. Outputs an object with log array of snapshots and timestamps, plus undo/redo/clear methods. Limited by manual logging triggers. ```typescript import { StateHistory } from "runed"; let count = $state(0); const history = new StateHistory(() => count, (c) => (count = c)); history.log[0]; // { snapshot: 0, timestamp: ... } ``` -------------------------------- ### Resource Pre-render Execution Source: https://runed.dev/docs/utilities/resource Demonstrates how to use `resource.pre()` for pre-render execution of data fetching. This allows data to be fetched before the component is fully rendered, improving perceived performance. ```typescript const data = resource.pre( () => query, async (query) => { const res = await fetch(`/api/search?q=${query}`); return res.json(); } ); ``` -------------------------------- ### Throttled State Management with runed.dev Source: https://runed.dev/docs/utilities/throttled Demonstrates how to use the Throttled component from 'runed' to create a throttled state. It shows basic usage with an input field and displaying the throttled search query, along with how to bind to a state variable and a delay. Dependencies include the 'runed' library. ```typescriptYou searched for: {throttled.current}
{logged || "Press the button!"}
``` -------------------------------- ### Attach Automatically Disposed Event Listener with useEventListener Source: https://runed.dev/docs/utilities/use-event-listener This example demonstrates how to use the `useEventListener` function to attach an event listener to the document body. The listener automatically disposes when the component is unmounted or the target element changes. It's useful for global event tracking without direct DOM manipulation. ```typescript import { useEventListener } from "runed"; export class ClickLogger { #clicks = $state(0); constructor() { useEventListener( () => document.body, "click", () => this.#clicks++ ); } get clicks() { return this.#clicks; } } ``` ```svelteYou've clicked the document {logger.clicks} {logger.clicks === 1 ? "time" : "times"}
``` -------------------------------- ### FiniteStateMachine: Lifecycle Methods (_enter, _exit) Source: https://runed.dev/docs/utilities/finite-state-machine Demonstrates the use of special lifecycle methods (_enter, _exit) in FiniteStateMachine to execute logic when entering or exiting a state. These methods receive a metadata object. ```typescript import { FiniteStateMachine } from "runed"; type MyStates = "on" | "off"; type MyEvents = "toggle"; const f = new FiniteStateMachineYou searched for: {throttledSearch}
Previous: {`${previous.current}`}
Coords: {JSON.stringify(location.position.coords, null, 2)}
Located at: {location.position.timestamp}
Error: {JSON.stringify(location.error, null, 2)}
Is Supported: {location.isSupported}
```
--------------------------------
### Initialize ScrollState with an Element
Source: https://runed.dev/docs/utilities/scroll-state
Demonstrates how to initialize the ScrollState utility with a specific HTML element as the scroll container. It shows how to bind the element and access reactive properties like scroll position and direction.
```typescript
import { ScrollState } from "runed";
let el = $state