From 3dba9e6ba6659a27412b77a04e5e9fd8e5ba592e Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 28 Dec 2025 22:26:18 -0800 Subject: [PATCH] old boy --- examples/button.tsx | 8 +++--- examples/form.tsx | 57 ++++++++++++++++++++++++++++++++++++++-- examples/ssr/landing.tsx | 16 ++++++----- src/index.tsx | 13 +++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) diff --git a/examples/button.tsx b/examples/button.tsx index f664fa9..322b287 100644 --- a/examples/button.tsx +++ b/examples/button.tsx @@ -1,7 +1,9 @@ -import { define } from '../src' +import { createScope } from '../src' import { ExampleSection } from './ssr/helpers' -const Button = define('Button', { +const { define } = createScope('Button') + +const Button = define('Root', { base: 'button', padding: "12px 24px", @@ -73,7 +75,7 @@ const Button = define('Button', { }, }) -const ButtonRow = define('ButtonRow', { +const ButtonRow = define('Row', { display: 'flex', gap: 16, flexWrap: 'wrap', diff --git a/examples/form.tsx b/examples/form.tsx index e5c0370..7bf47e0 100644 --- a/examples/form.tsx +++ b/examples/form.tsx @@ -156,8 +156,56 @@ const Checkbox = define('Checkbox', { } }) +const FormExamples = define('FormExamples', { + maxWidth: 600, + margin: '0 auto' +}) + +const Button = define('FormButton', { + base: 'button', + + padding: '12px 24px', + fontSize: 16, + fontWeight: 600, + border: 'none', + borderRadius: 8, + cursor: 'pointer', + transition: 'all 0.2s ease', + background: '#3b82f6', + color: 'white', + + states: { + ':hover': { + background: '#2563eb' + }, + ':active': { + transform: 'translateY(1px)' + } + }, + + variants: { + variant: { + secondary: { + background: '#6b7280', + color: 'white', + states: { + ':hover': { + background: '#4b5563' + } + } + } + } + } +}) + +const ButtonGroup = define('FormButtonGroup', { + display: 'flex', + gap: 12, + marginTop: 24 +}) + export const FormExamplesContent = () => ( - <> + @@ -195,5 +243,10 @@ export const FormExamplesContent = () => ( - + + + + + + ) diff --git a/examples/ssr/landing.tsx b/examples/ssr/landing.tsx index 1f8152e..e10a343 100644 --- a/examples/ssr/landing.tsx +++ b/examples/ssr/landing.tsx @@ -1,6 +1,8 @@ -import { define, Styles } from '../../src' +import { createScope, Styles } from '../../src' -const Page = define('LandingPage', { +const { define } = createScope('Landing') + +const Page = define('Page', { base: 'body', margin: 0, @@ -13,12 +15,12 @@ const Page = define('LandingPage', { background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)', }) -const Container = define('LandingContainer', { +const Container = define('Container', { textAlign: 'center', color: 'white', }) -const Title = define('LandingTitle', { +const Title = define('Title', { base: 'h1', fontSize: 48, @@ -27,7 +29,7 @@ const Title = define('LandingTitle', { color: 'white', }) -const Subtitle = define('LandingSubtitle', { +const Subtitle = define('Subtitle', { base: 'p', fontSize: 20, @@ -35,14 +37,14 @@ const Subtitle = define('LandingSubtitle', { color: 'rgba(255, 255, 255, 0.9)', }) -const ButtonGroup = define('LandingButtonGroup', { +const ButtonGroup = define('ButtonGroup', { display: 'flex', gap: 50, justifyContent: 'center', flexWrap: 'wrap', }) -const ChoiceCard = define('LandingChoiceCard', { +const ChoiceCard = define('ChoiceCard', { base: 'a', display: 'block', diff --git a/src/index.tsx b/src/index.tsx index 132e1dd..db0ca30 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -195,8 +195,21 @@ function registerStyles(name: string, def: TagDef) { injectStylesInBrowser() } +// automatic names let anonComponents = 1 +// module-level scoping +export function createScope(scope: string) { + return { + define: (nameOrDef: string | TagDef, defIfNamed?: TagDef) => { + if (typeof nameOrDef === 'string') + return define(`${scope}${nameOrDef === 'Root' ? '' : nameOrDef}`, defIfNamed) + else + return define(`${scope}Def${anonComponents++}`, nameOrDef as TagDef) + } + } +} + // the main event export function define(nameOrDef: string | TagDef, defIfNamed?: TagDef) { const name = defIfNamed ? (nameOrDef as string) : `Def${anonComponents++}`