Exclusivobeta

Trend Chip <mv-trend-chip>

O pequeno chip “▲ +12.4% vs last week” para cards de KPI, dashboards, tabelas de preços e relatórios: passe a variação (value="0.124") ou dois valores (from / to) e ele exibe o valor com sinal pelo Intl como porcentagem, pontos percentuais, número simples, qualquer moeda ou qualquer unidade (“−32 ms”), com um período opcional. better="down" faz uma queda de churn, latência ou custo ser lida como positiva, e o tom é transmitido pela forma além da cor (triângulo cheio sobre um fundo suave para o positivo, triângulo vazado em um chip com contorno para o negativo, seta fina para o neutro, traço para estável), com uma faixa de tolerância que trata pequenas variações como praticamente estáveis. O que os outros não fazem: os leitores de tela ouvem uma frase correta (“Up 12.4% vs last week, an improvement”, “Down 0.3 percentage points vs last month, an improvement”) em vez de “plus 12 percent up arrow”; o sinal e o arredondamento estão certos (um verdadeiro sinal de menos, nunca “−0.0%”, uma variação pequena demais para a precisão mostra “<0.1%” em vez de um zero enganoso); e ele funciona sem JS: o texto do servidor dentro dele é mantido até o script rodar, o atributo value sozinho já define o tom e a seta em CSS, e uma exportação pura describe() devolve o mesmo texto, a mesma frase e o mesmo tom para a renderização no servidor.

CategoriaExibição de dados
TipoWeb Component (<mv-trend-chip>)
Statusbeta
Keywordsexclusive, light, trend, delta, change, kpi, dashboard, percent, badge, intl, ssr, a11y

When to use

  • A KPI card or dashboard tile shows how a figure moved since the previous period
  • A report or pricing table lists changes where some metrics are better when they go down, like churn, cost or latency
  • A change badge must not rely on color alone and must be read as a sentence by screen readers
  • Server-rendered pages should print the change in HTML and keep it readable before or without JavaScript

Avoid when

  • The change must be judged against inflation, a benchmark or peers rather than against zero → use Real Terms instead
  • A whole section of headline figures with counters and sparklines is needed → use Stats instead
  • The number itself should count up or roll when it changes → use Number Ticker instead

Instalação

node scripts/add.mjs trend-chip --out ./src/marvelous

Agente de IA com o servidor MCP do Marvelous UI: install_components({ slugs: ["trend-chip"], target_dir: "<absolute path>/src/marvelous", framework: "react" }).

Arquivos copiados (dependências incluídas): tokens/tokens.css, core/base.css, core/element.js, components/trend-chip/trend-chip.js, components/trend-chip/trend-chip.css.

Uso

Início rápido, a menor marcação que funciona:

<mv-trend-chip value="0.124" period="vs last week"></mv-trend-chip>

Marcação de referência, para usar como ponto de partida e personalizar com atributos, data-* e variáveis CSS:

<div id="mv-tc-demo" style="display:grid;gap:1.25rem;width:min(100%,52rem);margin-inline:auto">
  <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(11.5rem,1fr));gap:.75rem">
    <section style="display:grid;gap:.45rem;padding:1rem;border:1px solid var(--mv-border);border-radius:var(--mv-radius-lg);background:var(--mv-surface);box-shadow:var(--mv-shadow-xs)">
      <h3 style="margin:0;font-size:.8rem;font-weight:500;color:var(--mv-fg-muted)">Revenue</h3>
      <p style="margin:0;font-size:1.5rem;font-weight:600;letter-spacing:-.02em;font-variant-numeric:tabular-nums">$48,210</p>
      <mv-trend-chip id="mv-tc-revenue" value="0.124" period="vs last week">+12.4% vs last week</mv-trend-chip>
    </section>
    <section style="display:grid;gap:.45rem;padding:1rem;border:1px solid var(--mv-border);border-radius:var(--mv-radius-lg);background:var(--mv-surface);box-shadow:var(--mv-shadow-xs)">
      <h3 style="margin:0;font-size:.8rem;font-weight:500;color:var(--mv-fg-muted)">Churn rate</h3>
      <p style="margin:0;font-size:1.5rem;font-weight:600;letter-spacing:-.02em;font-variant-numeric:tabular-nums">2.1%</p>
      <mv-trend-chip from="0.024" to="0.021" format="points" better="down" period="vs last month"></mv-trend-chip>
    </section>
    <section style="display:grid;gap:.45rem;padding:1rem;border:1px solid var(--mv-border);border-radius:var(--mv-radius-lg);background:var(--mv-surface);box-shadow:var(--mv-shadow-xs)">
      <h3 style="margin:0;font-size:.8rem;font-weight:500;color:var(--mv-fg-muted)">p95 latency</h3>
      <p style="margin:0;font-size:1.5rem;font-weight:600;letter-spacing:-.02em;font-variant-numeric:tabular-nums">214 ms</p>
      <mv-trend-chip from="182" to="214" format="millisecond" better="down" period="vs last week"></mv-trend-chip>
    </section>
    <section style="display:grid;gap:.45rem;padding:1rem;border:1px solid var(--mv-border);border-radius:var(--mv-radius-lg);background:var(--mv-surface);box-shadow:var(--mv-shadow-xs)">
      <h3 style="margin:0;font-size:.8rem;font-weight:500;color:var(--mv-fg-muted)">Active users</h3>
      <p style="margin:0;font-size:1.5rem;font-weight:600;letter-spacing:-.02em;font-variant-numeric:tabular-nums">12,480</p>
      <mv-trend-chip value="-0.036" period="vs last week"></mv-trend-chip>
    </section>
  </div>

  <div style="display:flex;flex-wrap:wrap;align-items:center;gap:.75rem;font-size:.8125rem;color:var(--mv-fg-muted)">
    <button class="mv-button" data-variant="outline" data-size="sm" id="mv-tc-refresh">Load next week</button>
    <span>Screen readers hear: <q id="mv-tc-said" style="color:var(--mv-fg)"></q></span>
  </div>

  <div class="mv-table-wrap">
    <table class="mv-table">
      <caption>September report. Plain variant; costs and refunds are lower-is-better.</caption>
      <thead><tr><th>Metric</th><th data-align="end">September</th><th data-align="end">Change vs August</th></tr></thead>
      <tbody>
        <tr><td>Support cost</td><td data-align="end">$17,950</td><td data-align="end"><mv-trend-chip data-variant="plain" id="mv-tc-cost" from="18400" to="17950" format="currency" better="down"></mv-trend-chip></td></tr>
        <tr><td>Sign-ups</td><td data-align="end">1,204,410</td><td data-align="end"><mv-trend-chip data-variant="plain" from="1204300" to="1204410"></mv-trend-chip></td></tr>
        <tr><td>Refund rate (±0.5% counts as flat)</td><td data-align="end">1.9%</td><td data-align="end"><mv-trend-chip data-variant="plain" id="mv-tc-refunds" value="0.003" better="down"></mv-trend-chip></td></tr>
        <tr><td>Uptime</td><td data-align="end">99.95%</td><td data-align="end"><mv-trend-chip data-variant="plain" from="0.9995" to="0.9995" format="points"></mv-trend-chip></td></tr>
        <tr><td>Headcount</td><td data-align="end">45</td><td data-align="end"><mv-trend-chip data-variant="plain" from="42" to="45" format="number" better="neutral"></mv-trend-chip></td></tr>
      </tbody>
    </table>
  </div>
</div>
<script type="module">
  const root = document.getElementById("mv-tc-demo");
  const revenue = root.querySelector("#mv-tc-revenue");
  const said = root.querySelector("#mv-tc-said");
  root.querySelector("#mv-tc-cost").digits = 0;
  root.querySelector("#mv-tc-refunds").tolerance = 0.005;
  const weeks = ["0.124", "-0.048", "0.0004", "0"];
  let i = 0;
  const show = () => customElements.whenDefined("mv-trend-chip").then(() => { said.textContent = revenue.state.label; });
  root.querySelector("#mv-tc-refresh").addEventListener("click", () => {
    i = (i + 1) % weeks.length;
    revenue.value = weeks[i];
    show();
  });
  show();
</script>

API

Attributes

NameTipoDefaultDescription
valuenumberThe change. For percent and points it is a fraction (0.124 = +12.4%, 0.003 = +0.3 pts); for number, currency and units it is the difference itself. Takes precedence over from / to.
from / tonumberOr give the previous and current amounts: percent computes (to − from) / |from| (from = 0 gives no comparison), the other formats use to − from. For points, give rates as fractions (0.024 → 0.021).
formatpercent | points | number | currency | ISO 4217 code | Intl unitpercentHow the change is printed, always through Intl: “+12.4%”, “−0.3 pts”, “+140”, “−$450.00” (currency = USD, or a code such as EUR), or a unit such as millisecond, kilobyte or hour (“−32 ms”).
periodstringWhat the change is measured against, shown after the chip and placed in the sentence: “vs last week”.
betterup | down | neutralupWhich direction is an improvement. down: a fall reads as good (churn, cost, latency). neutral: no judgment, only the direction.
lang (inherited)BCP 47Number locale from the nearest [lang] ancestor, else en-US.
data-variantplainStyling hook: colored text and glyph without the pill, for dense tables and inline copy (bad changes get a dotted underline as a second cue).
data-dir / data-toneup | down | flat | none / good | bad | neutralSet by the component (for styling). A server can write them too, from describe().

Properties

NameTipoDefaultDescription
tolerancenumber0A change whose size is at or below this (same units as value: 0.005 = half a percent) is about flat: dash glyph, neutral tone, and a sentence that still says which way it moved (“About flat vs last month, up 0.3%”).
digitsnumber1 for percent and points, the currency's own for money, up to 1 otherwiseFraction digits. Percent and points always print exactly this many, so every chip on a page rounds the same way.
stringsobjectMvTrendChip.stringsTranslatable text, merged over the defaults: whole sentences upGood, upBad, up, downGood, downBad, down, flat, nearUp, nearDown, none with {value} and {period} placeholders, plus under (“<{value}”), underLong (“less than {value}”), points, pointsLong and empty (“-”).
state{ dir, tone, text, period, label, change }Read-only: the current result, including the spoken sentence (label) and the raw change.
value / from / to / format / period / betterstringMirror the attributes; changes re-render at once. All can be set before the element is defined.

Methods

NameDescription
describe({ value, from, to, format, period, better, tolerance, digits, locale, strings })Named export (and static MvTrendChip.describe): pure and server-safe, returns { dir, tone, text, period, label, change }, the exact visible text and sentence the element renders.
render()Re-render now (rarely needed: attribute and property changes already do it).

Content structure

NameDescription
(text)Server-rendered fallback, read as is before the script loads; replaced by the parts once it runs. Without value or from / to, the text is left untouched.
.mv-trend-chip-value / -period / -labelOr render the final parts on the server (from describe()): they are adopted in place, so nothing shifts on upgrade.

CSS classes

NameDescription
mv-trend-chip-valueThe pill: glyph (::before, a CSS mask) and signed value; aria-hidden.
mv-trend-chip-periodThe muted period text; aria-hidden (it is part of the sentence).
mv-trend-chip-labelThe visually hidden sentence read by screen readers (mv-sr-only).

CSS variables

NameDefaultDescription
--mv-trend-chip-goodvar(--mv-success)Good tone.
--mv-trend-chip-badvar(--mv-danger)Bad tone.
--mv-trend-chip-neutralvar(--mv-fg-muted)Neutral and flat tone.
--mv-trend-chip-font-sizevar(--mv-text-xs)Text size (the glyph scales with it).
--mv-trend-chip-radiusvar(--mv-radius-full)Pill corner radius.
--mv-trend-chip-gap0.4emSpace between the pill and the period.

Accessibility

Plain inline content, no role and no tab stop. The visible pill and period are aria-hidden and a visually hidden span carries one complete sentence built from a whole-sentence template (never glued fragments): “Up 12.4% vs last week, an improvement”, “Down 32 ms vs last week, an improvement” with better="down", “Up 3” with better="neutral", “Unchanged vs yesterday”, “About flat vs last month, up 0.3%” inside the tolerance, “Up less than 0.1% vs yesterday, an improvement” for tiny changes, and “No comparison available” when the change cannot be computed. The number in the sentence is unsigned (the word carries the direction), so nothing reads “plus” or “minus” or “up arrow”. Updates are silent (no live region): a dashboard refreshing its tiles never interrupts the reader, and the text is only written when it actually changes. Meaning never depends on color alone (WCAG 1.4.1): direction is the glyph's orientation, good is a filled triangle, bad is a hollow triangle in an outlined chip (plus a dotted underline in the plain variant), neutral is a thin arrow and flat is a dash. Text colors are mixed toward the foreground for contrast on the tinted pill, in light and dark themes. Forced colors: the glyph is drawn in CanvasText, the chip gets a border, dashed when the change is bad. The glyph's turn when the direction flips uses the motion tokens and is removed under prefers-reduced-motion and [data-motion="reduce"]. Before or without JS, server-rendered text inside the element is read as written, and CSS already derives tone and direction from the value attribute.

Esta página foi traduzida com IA. Informar um problema de tradução