265 lines
14 KiB
Plaintext
265 lines
14 KiB
Plaintext
page ToggleCountcomponent {
|
|
seo {
|
|
title = "ToggleCount component"
|
|
description = "Theme-aware, responsive toggle count component."
|
|
}
|
|
|
|
view {
|
|
<div class="docs-shell">
|
|
<SkipLink label="Skip to content" href="#main" class="docs-skip-link" />
|
|
<header class="topbar">
|
|
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
|
|
<nav aria-label="Primary"><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/components">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
|
|
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.5.0</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
|
|
</header>
|
|
<div class="mobile-doc-nav"><details><summary>Browse documentation</summary><nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/components">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a><a href="/tutorial">Tutorial</a><a href="/guides/project-structure">Guides</a><a href="/examples">Examples</a><a href="/search">Search</a></nav></details></div>
|
|
<main class="portal-main docs-layout component-detail-page"><article id="main" class="documentation prose standalone"><nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a><span>/</span><a href="/components">Components</a><span>/</span><span aria-current="page">ToggleCount</span></nav><section class="doc-intro"><span class="eyebrow">advanced-forms component</span><h1>ToggleCount</h1><p>Theme-aware, responsive toggle count component.</p><div class="doc-meta"><span>@wrnexus/ui 0.5.0</span><span>23 props</span><span>0 slots</span><span>2 events</span></div></section><section id="usage"><h2>Usage</h2><p>Components are auto-discovered. Use the component directly in any <code>.wrn</code> view:</p><pre data-language="wrn"><code><ToggleCount
|
|
size="default"
|
|
color="primary"
|
|
variant="segmented"
|
|
name="billing-cycle"
|
|
value="monthly"
|
|
/></code></pre><p>Legacy mount name: <code>data-component="ToggleCount"</code>.</p></section><section id="props"><h2>Props and attributes</h2><div class="table-wrap"><table><thead><tr><th>Prop</th><th>Type</th><th>Requirement</th><th>Default</th></tr></thead><tbody><tr><td><code>size</code></td><td><code>string</code></td><td>Optional</td><td><code>"default"</code></td></tr><tr><td><code>color</code></td><td><code>string</code></td><td>Optional</td><td><code>"primary"</code></td></tr><tr><td><code>variant</code></td><td><code>string</code></td><td>Optional</td><td><code>"segmented"</code></td></tr><tr><td><code>class</code></td><td><code>string</code></td><td>Optional</td><td><code>""</code></td></tr><tr><td><code>name</code></td><td><code>string</code></td><td>Optional</td><td><code>"billing-cycle"</code></td></tr><tr><td><code>value</code></td><td><code>string</code></td><td>Optional</td><td><code>"monthly"</code></td></tr><tr><td><code>firstValue</code></td><td><code>string</code></td><td>Optional</td><td><code>"monthly"</code></td></tr><tr><td><code>firstLabel</code></td><td><code>string</code></td><td>Optional</td><td><code>"Monthly"</code></td></tr><tr><td><code>secondValue</code></td><td><code>string</code></td><td>Optional</td><td><code>"annual"</code></td></tr><tr><td><code>secondLabel</code></td><td><code>string</code></td><td>Optional</td><td><code>"Annual"</code></td></tr><tr><td><code>ariaLabel</code></td><td><code>string</code></td><td>Optional</td><td><code>"Billing frequency"</code></td></tr><tr><td><code>items</code></td><td><code>string</code></td><td>Optional</td><td><code>[]</code></td></tr><tr><td><code>currency</code></td><td><code>string</code></td><td>Optional</td><td><code>"$"</code></td></tr><tr><td><code>suffix</code></td><td><code>string</code></td><td>Optional</td><td><code>""</code></td></tr><tr><td><code>firstValueKey</code></td><td><code>string</code></td><td>Optional</td><td><code>"monthly"</code></td></tr><tr><td><code>secondValueKey</code></td><td><code>string</code></td><td>Optional</td><td><code>"annual"</code></td></tr><tr><td><code>emptyValue</code></td><td><code>string</code></td><td>Optional</td><td><code>"—"</code></td></tr><tr><td><code>align</code></td><td><code>string</code></td><td>Optional</td><td><code>"end"</code></td></tr><tr><td><code>fullWidth</code></td><td><code>boolean</code></td><td>Optional</td><td><code>true</code></td></tr><tr><td><code>disabled</code></td><td><code>boolean</code></td><td>Optional</td><td><code>false</code></td></tr><tr><td><code>animate</code></td><td><code>boolean</code></td><td>Optional</td><td><code>true</code></td></tr><tr><td><code>animationDuration</code></td><td><code>number</code></td><td>Optional</td><td><code>450</code></td></tr><tr><td><code>animationSteps</code></td><td><code>number</code></td><td>Optional</td><td><code>18</code></td></tr></tbody></table></div></section><section id="slots"><h2>Slots</h2><p>This component does not declare a slot.</p></section><section id="events"><h2>Events</h2><ul><li><code>change</code></li><li><code>toggle</code></li></ul></section><section id="source"><h2>Source contract</h2><p>Installed source: <code>components/ToggleCount.wrn</code></p><pre data-language="wrn"><code>component ToggleCount {
|
|
props {
|
|
size = "default"
|
|
color = "primary"
|
|
variant = "segmented"
|
|
class = ""
|
|
|
|
name = "billing-cycle"
|
|
value = "monthly"
|
|
firstValue = "monthly"
|
|
firstLabel = "Monthly"
|
|
secondValue = "annual"
|
|
secondLabel = "Annual"
|
|
ariaLabel = "Billing frequency"
|
|
|
|
items = []
|
|
currency = "$"
|
|
suffix = ""
|
|
firstValueKey = "monthly"
|
|
secondValueKey = "annual"
|
|
emptyValue = "—"
|
|
|
|
align = "end"
|
|
fullWidth = true
|
|
disabled = false
|
|
animate = true
|
|
animationDuration = 450
|
|
animationSteps = 18
|
|
|
|
@event change = function
|
|
@event toggle = function
|
|
}
|
|
|
|
state selectedValue = value
|
|
|
|
functions {
|
|
function isFirstSelected() {
|
|
return selectedValue === firstValue
|
|
}
|
|
|
|
function isSecondSelected() {
|
|
return selectedValue === secondValue
|
|
}
|
|
|
|
function displayValue(item) {
|
|
if (isSecondSelected()) {
|
|
return item[secondValueKey] !== undefined
|
|
? item[secondValueKey]
|
|
: emptyValue
|
|
}
|
|
|
|
return item[firstValueKey] !== undefined
|
|
? item[firstValueKey]
|
|
: emptyValue
|
|
}
|
|
|
|
function dispatchToggleEvent(sourceEvent, previousValue, root, customEvent) {
|
|
root = sourceEvent.currentTarget.closest("[data-wrn-toggle-count]")
|
|
|
|
if (!root) {
|
|
return
|
|
}
|
|
|
|
customEvent = document.createEvent("CustomEvent")
|
|
customEvent.initCustomEvent("change", true, false, {
|
|
component: "ToggleCount",
|
|
name: name,
|
|
value: selectedValue,
|
|
previousValue: previousValue,
|
|
firstValue: firstValue,
|
|
secondValue: secondValue
|
|
})
|
|
root.dispatchEvent(customEvent)
|
|
|
|
customEvent = document.createEvent("CustomEvent")
|
|
customEvent.initCustomEvent("toggle", true, false, {
|
|
component: "ToggleCount",
|
|
name: name,
|
|
value: selectedValue,
|
|
previousValue: previousValue,
|
|
firstValue: firstValue,
|
|
secondValue: secondValue
|
|
})
|
|
root.dispatchEvent(customEvent)
|
|
}
|
|
|
|
function selectValue(sourceEvent, nextValue, previousValue) {
|
|
if (disabled || selectedValue === nextValue) {
|
|
return
|
|
}
|
|
|
|
previousValue = selectedValue
|
|
selectedValue = nextValue
|
|
|
|
if (
|
|
animate &&
|
|
!window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
) {
|
|
animateDisplayedValues(sourceEvent, previousValue)
|
|
}
|
|
|
|
dispatchToggleEvent(sourceEvent, previousValue)
|
|
}
|
|
|
|
function animateDisplayedValues(sourceEvent, previousValue, root, nodes) {
|
|
root = sourceEvent.currentTarget.closest("[data-wrn-toggle-count]")
|
|
|
|
if (!root) {
|
|
return
|
|
}
|
|
|
|
nodes = root.querySelectorAll("[data-toggle-count-value]")
|
|
animateValueAt(nodes, 0, previousValue)
|
|
}
|
|
|
|
function animateValueAt(nodes, index, previousValue, node, fromValue, toValue) {
|
|
if (index >= nodes.length) {
|
|
return
|
|
}
|
|
|
|
node = nodes[index]
|
|
fromValue = Number(
|
|
previousValue === secondValue
|
|
? node.getAttribute("data-second-value")
|
|
: node.getAttribute("data-first-value")
|
|
)
|
|
toValue = Number(
|
|
selectedValue === secondValue
|
|
? node.getAttribute("data-second-value")
|
|
: node.getAttribute("data-first-value")
|
|
)
|
|
|
|
if (!Number.isNaN(fromValue) && !Number.isNaN(toValue)) {
|
|
animateValueFrame(node, fromValue, toValue, 0)
|
|
}
|
|
|
|
animateValueAt(nodes, index + 1, previousValue)
|
|
}
|
|
|
|
function animateValueFrame(node, fromValue, toValue, frame, totalFrames, nextValue) {
|
|
totalFrames = Math.max(1, Number(animationSteps) || 1)
|
|
|
|
if (frame >= totalFrames) {
|
|
node.replaceChildren(String(toValue))
|
|
return
|
|
}
|
|
|
|
nextValue = Math.round(
|
|
fromValue +
|
|
(toValue - fromValue) * (frame / totalFrames)
|
|
)
|
|
node.replaceChildren(String(nextValue))
|
|
|
|
setTimeout(
|
|
animateValueFrame,
|
|
Math.max(1, Number(animationDuration) / totalFrames),
|
|
node,
|
|
fromValue,
|
|
toValue,
|
|
frame + 1
|
|
)
|
|
}
|
|
|
|
function toggleValue(sourceEvent) {
|
|
selectValue(
|
|
sourceEvent,
|
|
isFirstSelected() ? secondValue : firstValue
|
|
)
|
|
}
|
|
}
|
|
|
|
view {
|
|
<section
|
|
{...attrs}
|
|
data-wrn-toggle-count
|
|
data-variant="{variant}"
|
|
data-value="{selectedValue}"
|
|
data-disabled="{disabled ? 'true' : 'false'}"
|
|
class="wire-next wire-next--color-{color} wire-next--size-{size} wire-next--toggle-count wire-next--toggle-count-{variant} {fullWidth ? 'wire-next--toggle-count-full' : ''} {disabled ? 'wire-next--disabled' : ''} {class}"
|
|
>
|
|
<input type="hidden" name="{name}" value="{selectedValue}" />
|
|
<div class="wire-next__toggle-count-control wire-next__toggle-count-control--{align}">
|
|
{#if variant === "switch"}
|
|
<span data-selected="{isFirstSelected() ? 'true' : 'false'}">{firstLabel}</span>
|
|
<button
|
|
type="button"
|
|
role="switch"
|
|
aria-label="{ariaLabel}"
|
|
aria-checked="{isSecondSelected() ? 'true' : 'false'}"
|
|
disabled="{disabled}"
|
|
@click="toggleValue(event)"
|
|
class="wire-next__toggle-count-switch"
|
|
>
|
|
<span aria-hidden="true">
|
|
</span>
|
|
</button>
|
|
<span data-selected="{isSecondSelected() ? 'true' : 'false'}">{secondLabel}</span>
|
|
{:else}
|
|
<div class="wire-next__toggle-count-segmented" role="group" aria-label="{ariaLabel}">
|
|
<button
|
|
type="button"
|
|
aria-pressed="{isFirstSelected() ? 'true' : 'false'}"
|
|
disabled="{disabled}"
|
|
@click="selectValue(event, firstValue)"
|
|
>{firstLabel}</button>
|
|
<button
|
|
type="button"
|
|
aria-pressed="{isSecondSelected() ? 'true' : 'false'}"
|
|
disabled="{disabled}"
|
|
@click="selectValue(event, secondValue)"
|
|
>{secondLabel}</button>
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
|
|
{#if items.length > 0}
|
|
<div class="wire-next__toggle-count-items">
|
|
{#each items as item}
|
|
<article>
|
|
<span>{item.label || item.name}</span>
|
|
<strong>
|
|
{#if currency}<small>{currency}</small>{/if}
|
|
<span
|
|
data-toggle-count-value
|
|
data-first-value="{item[firstValueKey]}"
|
|
data-second-value="{item[secondValueKey]}"
|
|
>{displayValue(item)}</span>
|
|
{#if suffix}<small>{suffix}</small>{/if}
|
|
</strong>
|
|
{#if item.description}<small>{item.description}</small>{/if}
|
|
</article>
|
|
{/each}
|
|
</div>
|
|
{/if}
|
|
</section>
|
|
}
|
|
}
|
|
</code></pre></section></article><aside class="on-this-page"><h2>On this page</h2><nav><a href="#usage">Usage</a><a href="#props">Props and attributes</a><a href="#slots">Slots</a><a href="#events">Events</a><a href="#source">Source contract</a></nav></aside></main>
|
|
<footer><div class="footer-brand"><span class="footer-mark" aria-hidden="true">W</span><p><strong>WRNexusJS 0.5.0</strong><span>Complete API documentation generated from installed package declarations.</span></p></div><nav aria-label="Footer"><a href="/packages">All packages</a><a href="/getting-started">Get started</a><a href="/security">Security</a><a href="/support">Support</a><a href="/llms.txt">AI guide</a></nav><p class="footer-meta">Private Developer Preview · Bun-native</p></footer>
|
|
<BackToTop />
|
|
</div>
|
|
}
|
|
}
|