@notchapp/api 0.1.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/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @notchapp/api
2
+
3
+ Component API for building NotchApp widgets.
4
+
5
+ Install with:
6
+
7
+ ```bash
8
+ npm install @notchapp/api
9
+ ```
10
+
11
+ Use it in a widget:
12
+
13
+ ```tsx
14
+ import { Button, Stack, Text } from "@notchapp/api";
15
+
16
+ export const initialState = {
17
+ count: 0,
18
+ };
19
+
20
+ export const actions = {
21
+ increment(state) {
22
+ return {
23
+ ...state,
24
+ count: (state?.count ?? 0) + 1,
25
+ };
26
+ },
27
+ };
28
+
29
+ export default function Widget({ environment, state }) {
30
+ return (
31
+ <Stack spacing={10}>
32
+ <Text>Hello from NotchApp</Text>
33
+ <Text tone="secondary">{`Span ${environment.span} • Count ${state.count}`}</Text>
34
+ <Button title="Increment" action="increment" />
35
+ </Stack>
36
+ );
37
+ }
38
+ ```
39
+
40
+ Current exports:
41
+
42
+ - `Stack`
43
+ - `Inline`
44
+ - `Row`
45
+ - `Text`
46
+ - `Icon`
47
+ - `IconButton`
48
+ - `Checkbox`
49
+ - `Input`
50
+ - `Button`
51
+
52
+ The SDK source and examples live in the main repository:
53
+
54
+ <https://github.com/itstauq/NotchApp>
package/index.js ADDED
@@ -0,0 +1,139 @@
1
+ function flattenChildren(input) {
2
+ if (input == null || input === false) return [];
3
+ if (Array.isArray(input)) return input.flatMap(flattenChildren);
4
+ return [input];
5
+ }
6
+
7
+ function extractText(input) {
8
+ return flattenChildren(input)
9
+ .map((item) => {
10
+ if (typeof item === "string" || typeof item === "number") return String(item);
11
+ if (item && typeof item.text === "string") return item.text;
12
+ return "";
13
+ })
14
+ .join("");
15
+ }
16
+
17
+ function wrapNode(input) {
18
+ if (input == null || input === false) return null;
19
+ const flattened = flattenChildren(input);
20
+ if (flattened.length === 0) return null;
21
+ return { node: flattened[0] };
22
+ }
23
+
24
+ function Stack(props = {}) {
25
+ return {
26
+ id: props.id ?? undefined,
27
+ type: "Stack",
28
+ direction: props.direction ?? "vertical",
29
+ spacing: props.spacing ?? 8,
30
+ children: flattenChildren(props.children),
31
+ };
32
+ }
33
+
34
+ function Inline(props = {}) {
35
+ return {
36
+ id: props.id ?? undefined,
37
+ type: "Inline",
38
+ spacing: props.spacing ?? 8,
39
+ children: flattenChildren(props.children),
40
+ };
41
+ }
42
+
43
+ function Row(props = {}) {
44
+ return {
45
+ id: props.id ?? undefined,
46
+ type: "Row",
47
+ action: props.action ?? null,
48
+ payload: props.payload ?? null,
49
+ children: flattenChildren(props.children),
50
+ };
51
+ }
52
+
53
+ function Text(props = {}) {
54
+ return {
55
+ id: props.id ?? undefined,
56
+ type: "Text",
57
+ text: props.text ?? extractText(props.children),
58
+ role: props.role ?? undefined,
59
+ tone: props.tone ?? undefined,
60
+ lineClamp: props.lineClamp ?? undefined,
61
+ strikethrough: props.strikethrough ?? undefined,
62
+ children: [],
63
+ };
64
+ }
65
+
66
+ function Icon(props = {}) {
67
+ return {
68
+ id: props.id ?? undefined,
69
+ type: "Icon",
70
+ symbol: props.symbol ?? props.icon ?? props.name ?? undefined,
71
+ tone: props.tone ?? undefined,
72
+ children: [],
73
+ };
74
+ }
75
+
76
+ function IconButton(props = {}) {
77
+ return {
78
+ id: props.id ?? undefined,
79
+ type: "IconButton",
80
+ symbol: props.symbol ?? props.icon ?? props.name ?? undefined,
81
+ action: props.action ?? null,
82
+ payload: props.payload ?? null,
83
+ tone: props.tone ?? undefined,
84
+ disabled: props.disabled ?? false,
85
+ children: [],
86
+ };
87
+ }
88
+
89
+ function Checkbox(props = {}) {
90
+ return {
91
+ id: props.id ?? undefined,
92
+ type: "Checkbox",
93
+ checked: props.checked ?? false,
94
+ action: props.action ?? null,
95
+ payload: props.payload ?? null,
96
+ children: [],
97
+ };
98
+ }
99
+
100
+ function Input(props = {}) {
101
+ return {
102
+ id: props.id ?? undefined,
103
+ type: "Input",
104
+ value: props.value ?? "",
105
+ placeholder: props.placeholder ?? "",
106
+ changeAction: props.changeAction ?? null,
107
+ submitAction: props.submitAction ?? null,
108
+ leadingAccessory: wrapNode(props.leadingAccessory),
109
+ trailingAccessory: wrapNode(props.trailingAccessory),
110
+ children: [],
111
+ };
112
+ }
113
+
114
+ function Button(props = {}) {
115
+ return {
116
+ id: props.id ?? undefined,
117
+ type: "Button",
118
+ title: props.title ?? extractText(props.children),
119
+ action: props.action ?? null,
120
+ payload: props.payload ?? null,
121
+ children: [],
122
+ };
123
+ }
124
+
125
+ module.exports = {
126
+ Stack,
127
+ Inline,
128
+ Row,
129
+ Text,
130
+ Icon,
131
+ IconButton,
132
+ Checkbox,
133
+ Input,
134
+ Button,
135
+ __internal: {
136
+ flattenChildren,
137
+ extractText,
138
+ },
139
+ };
package/jsx-runtime.js ADDED
@@ -0,0 +1,28 @@
1
+ const { __internal } = require("./index.js");
2
+
3
+ const Fragment = Symbol.for("notch.fragment");
4
+
5
+ function jsx(type, props, key) {
6
+ if (type === Fragment) {
7
+ return __internal.flattenChildren(props?.children);
8
+ }
9
+
10
+ const merged = { ...(props ?? {}) };
11
+ if (key != null && merged.id == null) {
12
+ merged.id = String(key);
13
+ }
14
+
15
+ if (typeof type === "function") {
16
+ return type(merged);
17
+ }
18
+
19
+ throw new Error(`Unsupported JSX type: ${String(type)}`);
20
+ }
21
+
22
+ const jsxs = jsx;
23
+
24
+ module.exports = {
25
+ Fragment,
26
+ jsx,
27
+ jsxs,
28
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@notchapp/api",
3
+ "version": "0.1.0",
4
+ "description": "Widget component API for building NotchApp widgets",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "jsx-runtime.js"
9
+ ],
10
+ "keywords": [
11
+ "notchapp",
12
+ "widgets",
13
+ "api",
14
+ "jsx",
15
+ "macos"
16
+ ],
17
+ "license": "Apache-2.0",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/itstauq/NotchApp.git",
21
+ "directory": "sdk/packages/api"
22
+ },
23
+ "homepage": "https://github.com/itstauq/NotchApp#readme",
24
+ "bugs": {
25
+ "url": "https://github.com/itstauq/NotchApp/issues"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "exports": {
31
+ ".": "./index.js",
32
+ "./jsx-runtime": "./jsx-runtime.js"
33
+ }
34
+ }