@positive-intentions/dim
v0.1.1
Published
A React-inspired functional web components framework built on native Web Components
Maintainers
Readme
Live Demo & Documentation | Getting Started | Demo | API Reference
Overview
Dim is a lightweight framework that brings React's component model and hooks to native Web Components. Write familiar functional components with hooks while leveraging the power of web standards.
Experimental: This framework is in early development and is not production-ready. It's provided for educational and experimental purposes.
Upgrading? See MIGRATION.md for breaking API changes (
useStore, object attributes,useFS, crypto format). See CHANGELOG.md for release notes (created on the first publish).
Install
npm install @positive-intentions/dimFeatures
- React-like DX - Familiar hooks API (
useState,useEffect,useMemo, etc.) - Web Standards - Built on native Web Components and Shadow DOM
- Scoped Styling - CSS-in-JS with automatic style isolation
- Zero runtime deps - Own template renderer; pure JS for the core (no React on the main entry)
- Lightweight - Parts-based updates preserve focus, selection, and child state
- State Persistence - Built-in global state management with
useStore
Getting Started
Quick Example
import { html, css, define, useState, useStyle } from "@positive-intentions/dim";
// Create a component
const Counter = (props, { useState, html, css, useStyle }) => {
const [count, setCount] = useState(0);
useStyle(css`
.counter {
padding: 2rem;
text-align: center;
}
button {
background: #029cfd;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
}
`);
return html`
<div class="counter">
<h2>Count: ${count}</h2>
<button @click="${() => setCount(count + 1)}">Increment</button>
</div>
`;
};
// Register as custom element
define({ tag: "my-counter", component: Counter });
// Use it
// <my-counter></my-counter>React interop
The main package entry does not import React, so it works with native ES modules in the browser. For the React hook that shares Dim's encrypted store:
import { useDimStore } from "@positive-intentions/dim/react";react is an optional peer dependency required only when you use this subpath.
Core Concepts
Hooks
Dim provides React-like hooks for managing component logic:
useState - Local State Management
const [value, setValue] = useState(initialValue);useEffect - Side Effects
useEffect(() => {
// Effect logic
return () => {
// Cleanup
};
}, [dependencies]);useStyle - Scoped CSS
useStyle(css`
.my-class {
color: blue;
}
`);useScope - Component Composition
// Hyphenated tag keys
useScope({
"child-component": ChildComponent,
});
// Or tagged functions after define() stamped `.tag`
define({ tag: "child-component", component: ChildComponent });
useScope({ ChildComponent });useMemo - Memoized Values
const expensive = useMemo(() => {
return computeExpensive(value);
}, [value]);useRef - DOM References
const inputRef = useRef();
// Access via inputRef.currentuseStore - Global State
const {
user: [user, setUser, isLoading, , isSaving],
} = useStore(
{ user: useState({ name: "John" }) },
{ encryptionKey: "optional-secret" },
);
// isSaving is true while a debounced IndexedDB persist is in flightExamples
Todo List
const TodoList = (props, { useState, html, css, useStyle }) => {
const [todos, setTodos] = useState([]);
const [input, setInput] = useState("");
const addTodo = () => {
if (input.trim()) {
setTodos([
...todos,
{
id: Date.now(),
text: input,
done: false,
},
]);
setInput("");
}
};
return html`
<div>
<input
.value="${input}"
@input="${(e) => setInput(e.target.value)}"
@keydown="${(e) => e.key === "Enter" && addTodo()}"
/>
<button @click="${addTodo}">Add</button>
<ul>
${todos.map(
(todo) => html`
<li class="${todo.done ? "done" : ""}">
<input
type="checkbox"
.checked="${todo.done}"
@change="${() => toggleTodo(todo.id)}"
/>
${todo.text}
</li>
`
)}
</ul>
</div>
`;
};Documentation
The framework includes comprehensive documentation through Storybook:
Main Sections
- Getting Started - Introduction and quick start guide
- Core Concepts - Deep dive into hooks and component architecture
- Tutorial - Step-by-step guide to building applications
- Examples - Real-world patterns and implementations
- API Reference - Detailed documentation for each hook
Hook Categories
- State Management:
useState,useStore - Effects & Lifecycle:
useEffect,useRef - Performance:
useMemo - Styling:
useStyle - Composition:
useScope
Visit the live documentation for interactive examples and detailed guides.
Development
Setup
# Clone the repository
git clone https://github.com/positive-intentions/dim.git
# Install dependencies
npm install
# Start Storybook dev server
npm run storybook
# Build the publishable library
npm run build:lib
# Build Storybook / federation docs site
npm run buildProject Structure
dim/
├── src/
│ ├── core/
│ │ ├── index.ts # Public API (published as @positive-intentions/dim)
│ │ ├── dim.ts # Compatibility shim (re-exports index)
│ │ ├── renderer/ # html/css tagged templates, parts, render()
│ │ ├── element/ # DimElement, define(), props helpers
│ │ ├── hooks/ # useState, useEffect, useMemo, useRef, useStyle, useScope
│ │ ├── store/ # useStore, crypto/storage/async managers
│ │ ├── transitions/ # view transitions + AutoTransitionHost
│ │ └── fs/ # useFS + File System Access handle persistence
│ └── stories/ # Storybook docs and demo apps
├── public/
└── README.mdReleasing
Releases are manual and Changeset-driven.
- In a PR that changes the public API or user-facing behavior, run
npx changeset, choose patch / minor / major, and write a short summary. - Merge the PR (with its changeset file) into
staging. - On GitHub: Actions → Publish → Run workflow (optionally enable dry_run first).
- The workflow versions the package, updates
CHANGELOG.md, publishes@positive-intentions/dimto npm with provenance, pushes avX.Y.Ztag, and creates a GitHub Release.
Prerequisites (one-time): create the @positive-intentions org on npm, enable Trusted Publishing for this repo’s publish.yml workflow, and create a GitHub Environment named npm (optional protection rules / required reviewers).
License
This project is licensed under the GNU General Public License v3.0 (GPL-3.0) — see the LICENSE file for details. You are free to use, modify, and distribute this software; distributed derivative works must also be licensed under GPL-3.0.
Acknowledgments
- Inspired by React's component model and hooks
- Built on native Web Components with Dim's own template renderer
- Community feedback
