Files
WRNexusJSDoc/app/pages/components/toggle-count.wrn
T

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>&lt;ToggleCount
size=&quot;default&quot;
color=&quot;primary&quot;
variant=&quot;segmented&quot;
name=&quot;billing-cycle&quot;
value=&quot;monthly&quot;
/&gt;</code></pre><p>Legacy mount name: <code>data-component=&quot;ToggleCount&quot;</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>&quot;default&quot;</code></td></tr><tr><td><code>color</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;primary&quot;</code></td></tr><tr><td><code>variant</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;segmented&quot;</code></td></tr><tr><td><code>class</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;&quot;</code></td></tr><tr><td><code>name</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;billing-cycle&quot;</code></td></tr><tr><td><code>value</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;monthly&quot;</code></td></tr><tr><td><code>firstValue</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;monthly&quot;</code></td></tr><tr><td><code>firstLabel</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;Monthly&quot;</code></td></tr><tr><td><code>secondValue</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;annual&quot;</code></td></tr><tr><td><code>secondLabel</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;Annual&quot;</code></td></tr><tr><td><code>ariaLabel</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;Billing frequency&quot;</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>&quot;$&quot;</code></td></tr><tr><td><code>suffix</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;&quot;</code></td></tr><tr><td><code>firstValueKey</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;monthly&quot;</code></td></tr><tr><td><code>secondValueKey</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;annual&quot;</code></td></tr><tr><td><code>emptyValue</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;—&quot;</code></td></tr><tr><td><code>align</code></td><td><code>string</code></td><td>Optional</td><td><code>&quot;end&quot;</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 &#123;
props &#123;
size = &quot;default&quot;
color = &quot;primary&quot;
variant = &quot;segmented&quot;
class = &quot;&quot;
name = &quot;billing-cycle&quot;
value = &quot;monthly&quot;
firstValue = &quot;monthly&quot;
firstLabel = &quot;Monthly&quot;
secondValue = &quot;annual&quot;
secondLabel = &quot;Annual&quot;
ariaLabel = &quot;Billing frequency&quot;
items = []
currency = &quot;$&quot;
suffix = &quot;&quot;
firstValueKey = &quot;monthly&quot;
secondValueKey = &quot;annual&quot;
emptyValue = &quot;—&quot;
align = &quot;end&quot;
fullWidth = true
disabled = false
animate = true
animationDuration = 450
animationSteps = 18
@event change = function
@event toggle = function
&#125;
state selectedValue = value
functions &#123;
function isFirstSelected() &#123;
return selectedValue === firstValue
&#125;
function isSecondSelected() &#123;
return selectedValue === secondValue
&#125;
function displayValue(item) &#123;
if (isSecondSelected()) &#123;
return item[secondValueKey] !== undefined
? item[secondValueKey]
: emptyValue
&#125;
return item[firstValueKey] !== undefined
? item[firstValueKey]
: emptyValue
&#125;
function dispatchToggleEvent(sourceEvent, previousValue, root, customEvent) &#123;
root = sourceEvent.currentTarget.closest(&quot;[data-wrn-toggle-count]&quot;)
if (!root) &#123;
return
&#125;
customEvent = document.createEvent(&quot;CustomEvent&quot;)
customEvent.initCustomEvent(&quot;change&quot;, true, false, &#123;
component: &quot;ToggleCount&quot;,
name: name,
value: selectedValue,
previousValue: previousValue,
firstValue: firstValue,
secondValue: secondValue
&#125;)
root.dispatchEvent(customEvent)
customEvent = document.createEvent(&quot;CustomEvent&quot;)
customEvent.initCustomEvent(&quot;toggle&quot;, true, false, &#123;
component: &quot;ToggleCount&quot;,
name: name,
value: selectedValue,
previousValue: previousValue,
firstValue: firstValue,
secondValue: secondValue
&#125;)
root.dispatchEvent(customEvent)
&#125;
function selectValue(sourceEvent, nextValue, previousValue) &#123;
if (disabled || selectedValue === nextValue) &#123;
return
&#125;
previousValue = selectedValue
selectedValue = nextValue
if (
animate &amp;&amp;
!window.matchMedia(&quot;(prefers-reduced-motion: reduce)&quot;).matches
) &#123;
animateDisplayedValues(sourceEvent, previousValue)
&#125;
dispatchToggleEvent(sourceEvent, previousValue)
&#125;
function animateDisplayedValues(sourceEvent, previousValue, root, nodes) &#123;
root = sourceEvent.currentTarget.closest(&quot;[data-wrn-toggle-count]&quot;)
if (!root) &#123;
return
&#125;
nodes = root.querySelectorAll(&quot;[data-toggle-count-value]&quot;)
animateValueAt(nodes, 0, previousValue)
&#125;
function animateValueAt(nodes, index, previousValue, node, fromValue, toValue) &#123;
if (index &gt;= nodes.length) &#123;
return
&#125;
node = nodes[index]
fromValue = Number(
previousValue === secondValue
? node.getAttribute(&quot;data-second-value&quot;)
: node.getAttribute(&quot;data-first-value&quot;)
)
toValue = Number(
selectedValue === secondValue
? node.getAttribute(&quot;data-second-value&quot;)
: node.getAttribute(&quot;data-first-value&quot;)
)
if (!Number.isNaN(fromValue) &amp;&amp; !Number.isNaN(toValue)) &#123;
animateValueFrame(node, fromValue, toValue, 0)
&#125;
animateValueAt(nodes, index + 1, previousValue)
&#125;
function animateValueFrame(node, fromValue, toValue, frame, totalFrames, nextValue) &#123;
totalFrames = Math.max(1, Number(animationSteps) || 1)
if (frame &gt;= totalFrames) &#123;
node.replaceChildren(String(toValue))
return
&#125;
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
)
&#125;
function toggleValue(sourceEvent) &#123;
selectValue(
sourceEvent,
isFirstSelected() ? secondValue : firstValue
)
&#125;
&#125;
view &#123;
&lt;section
&#123;...attrs&#125;
data-wrn-toggle-count
data-variant=&quot;&#123;variant&#125;&quot;
data-value=&quot;&#123;selectedValue&#125;&quot;
data-disabled=&quot;&#123;disabled ? 'true' : 'false'&#125;&quot;
class=&quot;wire-next wire-next--color-&#123;color&#125; wire-next--size-&#123;size&#125; wire-next--toggle-count wire-next--toggle-count-&#123;variant&#125; &#123;fullWidth ? 'wire-next--toggle-count-full' : ''&#125; &#123;disabled ? 'wire-next--disabled' : ''&#125; &#123;class&#125;&quot;
&gt;
&lt;input type=&quot;hidden&quot; name=&quot;&#123;name&#125;&quot; value=&quot;&#123;selectedValue&#125;&quot; /&gt;
&lt;div class=&quot;wire-next__toggle-count-control wire-next__toggle-count-control--&#123;align&#125;&quot;&gt;
&#123;#if variant === &quot;switch&quot;&#125;
&lt;span data-selected=&quot;&#123;isFirstSelected() ? 'true' : 'false'&#125;&quot;&gt;&#123;firstLabel&#125;&lt;/span&gt;
&lt;button
type=&quot;button&quot;
role=&quot;switch&quot;
aria-label=&quot;&#123;ariaLabel&#125;&quot;
aria-checked=&quot;&#123;isSecondSelected() ? 'true' : 'false'&#125;&quot;
disabled=&quot;&#123;disabled&#125;&quot;
@click=&quot;toggleValue(event)&quot;
class=&quot;wire-next__toggle-count-switch&quot;
&gt;
&lt;span aria-hidden=&quot;true&quot;&gt;
&lt;/span&gt;
&lt;/button&gt;
&lt;span data-selected=&quot;&#123;isSecondSelected() ? 'true' : 'false'&#125;&quot;&gt;&#123;secondLabel&#125;&lt;/span&gt;
&#123;:else&#125;
&lt;div class=&quot;wire-next__toggle-count-segmented&quot; role=&quot;group&quot; aria-label=&quot;&#123;ariaLabel&#125;&quot;&gt;
&lt;button
type=&quot;button&quot;
aria-pressed=&quot;&#123;isFirstSelected() ? 'true' : 'false'&#125;&quot;
disabled=&quot;&#123;disabled&#125;&quot;
@click=&quot;selectValue(event, firstValue)&quot;
&gt;&#123;firstLabel&#125;&lt;/button&gt;
&lt;button
type=&quot;button&quot;
aria-pressed=&quot;&#123;isSecondSelected() ? 'true' : 'false'&#125;&quot;
disabled=&quot;&#123;disabled&#125;&quot;
@click=&quot;selectValue(event, secondValue)&quot;
&gt;&#123;secondLabel&#125;&lt;/button&gt;
&lt;/div&gt;
&#123;/if&#125;
&lt;/div&gt;
&#123;#if items.length &gt; 0&#125;
&lt;div class=&quot;wire-next__toggle-count-items&quot;&gt;
&#123;#each items as item&#125;
&lt;article&gt;
&lt;span&gt;&#123;item.label || item.name&#125;&lt;/span&gt;
&lt;strong&gt;
&#123;#if currency&#125;&lt;small&gt;&#123;currency&#125;&lt;/small&gt;&#123;/if&#125;
&lt;span
data-toggle-count-value
data-first-value=&quot;&#123;item[firstValueKey]&#125;&quot;
data-second-value=&quot;&#123;item[secondValueKey]&#125;&quot;
&gt;&#123;displayValue(item)&#125;&lt;/span&gt;
&#123;#if suffix&#125;&lt;small&gt;&#123;suffix&#125;&lt;/small&gt;&#123;/if&#125;
&lt;/strong&gt;
&#123;#if item.description&#125;&lt;small&gt;&#123;item.description&#125;&lt;/small&gt;&#123;/if&#125;
&lt;/article&gt;
&#123;/each&#125;
&lt;/div&gt;
&#123;/if&#125;
&lt;/section&gt;
&#125;
&#125;
</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>
}
}