@fcomurs/theme 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +34 -2
- package/index.d.ts +137 -0
- package/index.js +64 -0
- package/package.json +12 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Foxses Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fcomurs/theme
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Write [fcomurs](https://fcomurs.com) theme sections as React components — the way you'd write them in Next.js.
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
// sections/hero/section.tsx
|
|
7
|
+
import { defineSection, text, image, Image } from '@fcomurs/theme';
|
|
8
|
+
import Reveal from '../../components/Reveal'; // 'use client' — becomes an island
|
|
9
|
+
|
|
10
|
+
export default defineSection({
|
|
11
|
+
name: 'Hero',
|
|
12
|
+
settings: {
|
|
13
|
+
heading: text({ label: 'Heading', default: 'New season' }),
|
|
14
|
+
picture: image({ label: 'Picture', phone: true }),
|
|
15
|
+
},
|
|
16
|
+
async data({ store }) {
|
|
17
|
+
return { products: await store.products({ sort: 'newest', limit: 4 }) };
|
|
18
|
+
},
|
|
19
|
+
render: ({ settings, data }) => (
|
|
20
|
+
<section>
|
|
21
|
+
<Reveal><h1>{settings.heading}</h1></Reveal>
|
|
22
|
+
<Image src={settings.picture} alt="" />
|
|
23
|
+
{data.products.map((p) => <a key={p.id} href={p.url}>{p.title}</a>)}
|
|
24
|
+
</section>
|
|
25
|
+
),
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- **Settings** are typed field builders (`text`, `image`, `product`, `collection`, `select` …); the store's visual editor builds its panel from them.
|
|
30
|
+
- **`data`** and **`render`** run on fcomurs's servers, once per page view. Components from files that start with `'use client'` are hydrated in the browser — React state, Motion (Framer Motion) and GSAP work there.
|
|
31
|
+
- fcomurs compiles the theme when you push it; you don't deploy anything. This package gives your editor the types.
|
|
32
|
+
|
|
33
|
+
Start a theme with `npx fcomurs-theme new my-theme --react`. Guide: https://doc.fcomurs.com/en/themes
|
|
34
|
+
|
|
35
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Types for @fcomurs/theme — settings, blocks, store data and render props, all inferred.
|
|
2
|
+
import type { ReactNode, ImgHTMLAttributes, AnchorHTMLAttributes } from 'react';
|
|
3
|
+
|
|
4
|
+
// ---- what the store hands a section
|
|
5
|
+
|
|
6
|
+
export interface Product {
|
|
7
|
+
id: string;
|
|
8
|
+
title: string;
|
|
9
|
+
slug: string;
|
|
10
|
+
/** Its page in the store, e.g. /products/linen-shirt */
|
|
11
|
+
url: string;
|
|
12
|
+
image: string;
|
|
13
|
+
price: number;
|
|
14
|
+
/** The old price when it's on sale; null otherwise. */
|
|
15
|
+
compareAtPrice: number | null;
|
|
16
|
+
inStock: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface Collection {
|
|
20
|
+
name: string;
|
|
21
|
+
slug: string;
|
|
22
|
+
url: string;
|
|
23
|
+
image: string;
|
|
24
|
+
/** Up to 12 of its products. */
|
|
25
|
+
products: Product[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface StoreInfo {
|
|
29
|
+
name: string;
|
|
30
|
+
logo: string;
|
|
31
|
+
phone: string;
|
|
32
|
+
email: string;
|
|
33
|
+
address: string;
|
|
34
|
+
currencySymbol: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Read-only store data for `data()`. Runs on the server; at most 20 calls and 5 seconds per section. */
|
|
38
|
+
export interface Store {
|
|
39
|
+
info: StoreInfo;
|
|
40
|
+
/** `limit`: 12 when not given, at most 24. */
|
|
41
|
+
products(query?: { collection?: string; sort?: 'newest' | 'price-asc' | 'price-desc' | 'best-selling'; limit?: number }): Promise<Product[]>;
|
|
42
|
+
product(slug: string): Promise<Product | null>;
|
|
43
|
+
/** With up to 12 of its products. */
|
|
44
|
+
collection(slug: string): Promise<Collection | null>;
|
|
45
|
+
/** Every active collection, without products (each one's are a collection(slug) away). */
|
|
46
|
+
collections(): Promise<Collection[]>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** An image setting's value: the picture, and its phone version when the field asks for one. */
|
|
50
|
+
export interface ImageValue {
|
|
51
|
+
src: string;
|
|
52
|
+
phone: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---- fields
|
|
56
|
+
|
|
57
|
+
interface FieldBase<K extends string, V> {
|
|
58
|
+
__field: true;
|
|
59
|
+
kind: K;
|
|
60
|
+
label?: string;
|
|
61
|
+
help?: string;
|
|
62
|
+
default?: V;
|
|
63
|
+
/** Shown above the field, to group related ones (e.g. "Layout"). */
|
|
64
|
+
group?: string;
|
|
65
|
+
/** @internal the value's type */
|
|
66
|
+
__value?: V;
|
|
67
|
+
}
|
|
68
|
+
type Field<K extends string, V, O = {}> = FieldBase<K, V> & O;
|
|
69
|
+
|
|
70
|
+
export function text(options?: Partial<FieldBase<'text', string>> & { placeholder?: string }): Field<'text', string>;
|
|
71
|
+
export function textarea(options?: Partial<FieldBase<'textarea', string>> & { placeholder?: string }): Field<'textarea', string>;
|
|
72
|
+
export function richtext(options?: Partial<FieldBase<'richtext', string>>): Field<'richtext', string>;
|
|
73
|
+
export function url(options?: Partial<FieldBase<'url', string>>): Field<'url', string>;
|
|
74
|
+
export function color(options?: Partial<FieldBase<'color', string>>): Field<'color', string>;
|
|
75
|
+
export function datetime(options?: Partial<FieldBase<'datetime', string>>): Field<'datetime', string>;
|
|
76
|
+
export function checkbox(options?: Partial<FieldBase<'checkbox', boolean>>): Field<'checkbox', boolean>;
|
|
77
|
+
export function number(options?: Partial<FieldBase<'number', number>> & { min?: number; max?: number; step?: number }): Field<'number', number>;
|
|
78
|
+
export function range(options?: Partial<FieldBase<'range', number>> & { min: number; max: number; step?: number; unit?: string }): Field<'range', number>;
|
|
79
|
+
/** An image; `{ phone: true }` adds an optional phone picture (screens up to 639px). */
|
|
80
|
+
export function image(options?: Partial<FieldBase<'image', string>> & { phone?: boolean }): Field<'image', ImageValue>;
|
|
81
|
+
export function product(options?: Partial<FieldBase<'product', string>>): Field<'product', Product | null>;
|
|
82
|
+
export function collection(options?: Partial<FieldBase<'collection', string>>): Field<'collection', Collection | null>;
|
|
83
|
+
export function collections(options?: Partial<FieldBase<'collections', string[]>>): Field<'collections', Collection[]>;
|
|
84
|
+
export function select<const V extends string>(
|
|
85
|
+
options: readonly (V | { value: V; label: string })[],
|
|
86
|
+
rest?: Partial<FieldBase<'select', V>>
|
|
87
|
+
): Field<'select', V>;
|
|
88
|
+
|
|
89
|
+
type AnyField = FieldBase<string, unknown>;
|
|
90
|
+
type Fields = Record<string, AnyField>;
|
|
91
|
+
/** The values `render` and `data` get, from the fields. */
|
|
92
|
+
export type SettingsOf<F extends Fields> = { [K in keyof F]: F[K] extends FieldBase<string, infer V> ? V : never };
|
|
93
|
+
|
|
94
|
+
export interface BlockKind<F extends Fields = Fields> {
|
|
95
|
+
__block: true;
|
|
96
|
+
settings: F;
|
|
97
|
+
label?: string;
|
|
98
|
+
}
|
|
99
|
+
/** A kind of block: block({ title: text() }, { label: 'Feature' }). */
|
|
100
|
+
export function block<const F extends Fields>(settings: F, options?: { label?: string }): BlockKind<F>;
|
|
101
|
+
|
|
102
|
+
type BlocksOf<B extends Record<string, BlockKind>> = { [K in keyof B]: { id: string; type: K; settings: SettingsOf<B[K]['settings']> } }[keyof B][];
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The theme's own settings (config/settings.json → the editor's "Theme settings"), by key.
|
|
106
|
+
* In CSS they're also var(--theme-<key in kebab-case>) — colours, numbers, choices.
|
|
107
|
+
*/
|
|
108
|
+
export type ThemeSettings = Record<string, string | number | boolean | null>;
|
|
109
|
+
|
|
110
|
+
// ---- sections
|
|
111
|
+
|
|
112
|
+
export interface SectionDefinition<F extends Fields, B extends Record<string, BlockKind>, D> {
|
|
113
|
+
/** The name merchants see in the editor. */
|
|
114
|
+
name: string;
|
|
115
|
+
description?: string;
|
|
116
|
+
settings?: F;
|
|
117
|
+
blocks?: B;
|
|
118
|
+
/** At most this many blocks (default 50). */
|
|
119
|
+
maxBlocks?: number;
|
|
120
|
+
/** Server only: the store data this section needs. Its result goes to `render` as `data`. */
|
|
121
|
+
data?(input: { settings: SettingsOf<F>; theme: ThemeSettings; store: Store }): Promise<D> | D;
|
|
122
|
+
/** Server only, once per page view. Components from 'use client' files become islands. */
|
|
123
|
+
render(props: { settings: SettingsOf<F>; blocks: BlocksOf<B>; data: D; section: { id: string }; store: StoreInfo; theme: ThemeSettings }): ReactNode;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function defineSection<const F extends Fields = {}, const B extends Record<string, BlockKind> = {}, D = undefined>(
|
|
127
|
+
definition: SectionDefinition<F, B, D>
|
|
128
|
+
): SectionDefinition<F, B, D> & { __section: true };
|
|
129
|
+
|
|
130
|
+
// ---- components
|
|
131
|
+
|
|
132
|
+
/** A picture with its phone version: <Image src={settings.image} alt="…" />. */
|
|
133
|
+
export function Image(props: Omit<ImgHTMLAttributes<HTMLImageElement>, 'src'> & { src: ImageValue | string }): ReactNode;
|
|
134
|
+
/** A link inside the store. */
|
|
135
|
+
export function Link(props: AnchorHTMLAttributes<HTMLAnchorElement> & { href: string }): ReactNode;
|
|
136
|
+
/** A price in the store's currency. */
|
|
137
|
+
export function Money(props: { amount: number; symbol?: string }): ReactNode;
|
package/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @fcomurs/theme — the runtime a TSX theme section imports (docs/theme-next-plan.md, stage 2).
|
|
2
|
+
// fcomurs bundles this file into each theme when it compiles it; React comes from the store.
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const { createElement } = require('react');
|
|
6
|
+
|
|
7
|
+
/** A field: what the merchant can change in the editor. `key` is filled in from its name in `settings`. */
|
|
8
|
+
const field = (kind) => (options = {}) => ({ __field: true, kind, ...options });
|
|
9
|
+
|
|
10
|
+
const text = field('text');
|
|
11
|
+
const textarea = field('textarea');
|
|
12
|
+
const richtext = field('richtext');
|
|
13
|
+
const url = field('url');
|
|
14
|
+
const color = field('color');
|
|
15
|
+
const datetime = field('datetime');
|
|
16
|
+
const checkbox = field('checkbox');
|
|
17
|
+
const product = field('product');
|
|
18
|
+
const collection = field('collection');
|
|
19
|
+
const collections = field('collections');
|
|
20
|
+
/** An image; `{ phone: true }` adds an optional phone picture (screens up to 639px). */
|
|
21
|
+
const image = field('image');
|
|
22
|
+
const number = field('number');
|
|
23
|
+
const range = field('range');
|
|
24
|
+
/** A choice: select(['left', 'center']) or select([{ value: 'left', label: 'Left' }]). */
|
|
25
|
+
const select = (options, rest = {}) => ({ __field: true, kind: 'select', options, ...rest });
|
|
26
|
+
|
|
27
|
+
/** A kind of block the section takes: block({ title: text() }, { label: 'Feature' }). */
|
|
28
|
+
const block = (settings, options = {}) => ({ __block: true, settings, ...options });
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A section of a theme. fcomurs reads `settings` and `blocks` when it builds the
|
|
32
|
+
* theme (the editor's panel comes from them), runs `data` on the server, then
|
|
33
|
+
* `render` — once, on the server. Components from 'use client' files become islands.
|
|
34
|
+
*/
|
|
35
|
+
const defineSection = (definition) => {
|
|
36
|
+
if (!definition || typeof definition.render !== 'function') throw new Error('defineSection needs a render function');
|
|
37
|
+
return { __section: true, ...definition };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const PHONE = '(max-width: 639px)';
|
|
41
|
+
|
|
42
|
+
/** A picture with its phone version (image({ phone: true })): phones get `phone`, wider screens `src`. */
|
|
43
|
+
function Image({ src, alt = '', className, width, height, loading = 'lazy', ...rest }) {
|
|
44
|
+
const value = src && typeof src === 'object' ? src : { src };
|
|
45
|
+
if (!value.src) return null;
|
|
46
|
+
const img = createElement('img', { src: value.src, alt, className, width, height, loading, ...rest });
|
|
47
|
+
if (!value.phone) return img;
|
|
48
|
+
return createElement('picture', { style: { display: 'contents' } }, createElement('source', { media: PHONE, srcSet: value.phone }), img);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A link inside the store (a plain <a>: every page of the store is drawn by fcomurs). */
|
|
52
|
+
function Link({ href, children, ...rest }) {
|
|
53
|
+
return createElement('a', { href: href || '/', ...rest }, children);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A price in the store's currency: <Money amount={p.price} />. */
|
|
57
|
+
function Money({ amount, symbol }) {
|
|
58
|
+
const v = Number(amount);
|
|
59
|
+
if (!Number.isFinite(v)) return null;
|
|
60
|
+
const sign = symbol || (typeof globalThis !== 'undefined' && globalThis.__fcCurrencySymbol) || '৳';
|
|
61
|
+
return createElement('span', null, `${sign}${v.toLocaleString('en-IN')}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = { defineSection, block, text, textarea, richtext, url, color, datetime, checkbox, product, collection, collections, image, number, range, select, Image, Link, Money };
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fcomurs/theme",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Write fcomurs theme sections as React components: defineSection, typed settings, store data, Image and Link.",
|
|
5
|
+
"keywords": ["fcomurs", "theme", "react", "nextjs", "ecommerce"],
|
|
6
|
+
"homepage": "https://doc.fcomurs.com/en/first-theme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"types": "index.d.ts",
|
|
10
|
+
"files": ["index.js", "index.d.ts", "README.md", "LICENSE"],
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"react": ">=19"
|
|
13
|
+
}
|
|
14
|
+
}
|