惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
The Cloudflare Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
I
InfoQ
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
H
Help Net Security
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
⭐ State.js Basics — Learn CSS‑Driven Reactivity in 10 Min...
iDev-Games · 2026-05-30 · via DEV Community
Cover image for ⭐ State.js Basics — Learn CSS‑Driven Reactivity in 10 Minutes

iDev-Games

State.js looks simple — just HTML attributes — but it introduces a new mental model for building UI:

HTML holds the data.

CSS reacts to the data.

State.js keeps them in sync.

If you’re used to React, Vue, or Svelte, this feels strange at first.

If you’re used to CSS, this feels like “why didn’t CSS always work like this?”

This tutorial will teach you the core ideas behind State.js so you can build reactive UI without JavaScript logic, without a framework, and without a build step.


⭐ 1. What State.js Actually Does

State.js turns HTML attributes into live CSS variables.

Example:

<div data-state data-count="0"></div>

State.js automatically exposes:

--state-count: 0;

If data-count changes, the CSS variable updates instantly.

This is the foundation of everything.

Learn more: Reactive attributes


⭐ 2. Your First Reactive Element

Let’s make a simple counter.

HTML

<div id="counter"
     data-state
     data-count="0"
     data-state-text="Count: {count}">
</div>

What’s happening?

  • data-state → activates State.js
  • data-count="0" → creates --state-count: 0
  • data-state-text="Count: {count}" → binds text to the value

State.js replaces {count} with the live value.


⭐ 3. Updating State with Triggers

Add a button that increments the counter:

<button
  data-state-trigger
  data-state-target="#counter"
  data-state-attr="count"
  data-state-increment="1">
  +1
</button>

What this means:

  • data-state-trigger → this element performs an action
  • data-state-target="#counter" → update that element
  • data-state-attr="count" → update the data-count attribute
  • data-state-increment="1" → add 1

Clicking the button updates:

  • the attribute
  • the CSS variable
  • the text
  • all automatically

Learn more: Triggers


⭐ 4. Styling with CSS Variables

Because State.js exposes attributes as CSS variables, you can style reactively:

#counter {
  color: hsl(calc(var(--state-count) * 20), 80%, 50%);
}

As the count increases, the color changes.

No JS.

No re-renders.

Just CSS reacting to state.

Learn more: CSS variable projection


⭐ 5. Conditions (Reactive Logic in HTML)

Let’s change the background when the count reaches 5.

<div id="counter"
     data-state
     data-count="0"
     data-state-class="big: count >= 5"
     data-state-text="Count: {count}">
</div>

This adds the class .big when the condition is true.

CSS:

.big {
  background: gold;
}

Learn more: Conditions


⭐ 6. Autofire (State.js “Game Loop”)

State.js can update values on an interval:

<div data-state
     data-time="0"
     data-state-autofire="time += 1 every 100ms"
     data-state-text="Time: {time}">
</div>

This creates a reactive timer with no JavaScript.

Learn more: Intervals


⭐ 7. Two-Way Binding (Forms Without JS)

<input type="range"
       data-state
       data-value="50"
       data-state-bind="value">

<div data-state
     data-state-text="Value: {value}">
</div>

Moving the slider updates the text automatically.

Learn more: Binding


⭐ 8. Putting It All Together — A Mini App

<div id="app" data-state data-count="0">
  <h1 data-state-text="Count: {count}"></h1>

  <button
    data-state-trigger
    data-state-target="#app"
    data-state-attr="count"
    data-state-increment="1">
    +1
  </button>

  <button
    data-state-trigger
    data-state-target="#app"
    data-state-attr="count"
    data-state-increment="-1">
    -1
  </button>

  <div data-state-class="warning: count < 0">
    <p>Count is negative!</p>
  </div>
</div>

What you get:

  • reactive text
  • reactive classes
  • reactive styling
  • reactive state
  • no JavaScript logic

This is the State.js mental model.


⭐ 9. Why This Matters

State.js gives you:

✔ Reactivity without JavaScript

No functions.

No hooks.

No re-renders.

✔ Components without frameworks

Just templates + includes.

✔ State without state management libraries

Attributes are the state.

✔ UI logic without JS

CSS handles behavior.

✔ Zero build step

Works in plain HTML.

This is why people say:

“It feels like something CSS should have done.”

Because State.js makes the browser behave the way developers wish it behaved.