create-marcapony-app 0.4.5 → 0.4.6
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-marcapony-app",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"description": "Scaffold a Marcapony app \u2014 a Worker backed by the Marcapony API, ready for `marcapony app link` and `marcapony app deploy`",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { StrictMode } from 'react'
|
|
2
2
|
import { createRoot } from 'react-dom/client'
|
|
3
3
|
import { App } from './App'
|
|
4
|
+
import { followPortalTheme } from './portal-theme'
|
|
4
5
|
import './styles.css'
|
|
5
6
|
|
|
7
|
+
followPortalTheme()
|
|
8
|
+
|
|
6
9
|
const root = document.getElementById('root')
|
|
7
10
|
if (!root) throw new Error('#root is missing from index.html')
|
|
8
11
|
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow the customer portal's theme when the app is shown inside the portal.
|
|
3
|
+
*
|
|
4
|
+
* The portal sends `{ type: 'mp:theme', theme }` to the framed app after it
|
|
5
|
+
* loads, when the app posts `{ type: 'mp:ready' }`, and whenever the visitor
|
|
6
|
+
* switches between light and dark or the owner edits the theme. This writes the
|
|
7
|
+
* colours, radius and font onto the CSS variables `styles.css` reads. Outside
|
|
8
|
+
* the portal nothing arrives and the defaults in `styles.css` apply.
|
|
9
|
+
*
|
|
10
|
+
* Only messages from the parent window are read, and every value is checked
|
|
11
|
+
* before it reaches a style.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const COLORS: ReadonlyArray<readonly [string, string]> = [
|
|
15
|
+
['background', '--background'],
|
|
16
|
+
['foreground', '--foreground'],
|
|
17
|
+
['card', '--card'],
|
|
18
|
+
['primary', '--primary'],
|
|
19
|
+
['primaryForeground', '--primary-foreground'],
|
|
20
|
+
['muted', '--muted'],
|
|
21
|
+
['mutedForeground', '--muted-foreground'],
|
|
22
|
+
['destructive', '--destructive'],
|
|
23
|
+
['border', '--border'],
|
|
24
|
+
['ring', '--ring'],
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
const RGB = /^rgb\(\d{1,3} \d{1,3} \d{1,3}\)$/
|
|
28
|
+
const RADIUS = /^\d+(\.\d+)?(rem|px|em)$/
|
|
29
|
+
const FONT_STACK = /^[\w\s"',-]{1,200}$/
|
|
30
|
+
|
|
31
|
+
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
32
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
33
|
+
? (value as Record<string, unknown>)
|
|
34
|
+
: null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function followPortalTheme(root: HTMLElement = document.documentElement): void {
|
|
38
|
+
if (window.parent === window) return
|
|
39
|
+
|
|
40
|
+
// A theme can leave out values the portal could not read: drop what the
|
|
41
|
+
// previous one set and this one omits, so the default from styles.css returns.
|
|
42
|
+
let applied: string[] = []
|
|
43
|
+
window.addEventListener('message', (event) => {
|
|
44
|
+
if (event.source !== window.parent) return
|
|
45
|
+
const message = asRecord(event.data)
|
|
46
|
+
if (message?.type !== 'mp:theme') return
|
|
47
|
+
const theme = asRecord(message?.theme)
|
|
48
|
+
if (!theme) return
|
|
49
|
+
|
|
50
|
+
const next = new Map<string, string>()
|
|
51
|
+
const colors = asRecord(theme.colors) ?? {}
|
|
52
|
+
for (const [key, variable] of COLORS) {
|
|
53
|
+
const value = colors[key]
|
|
54
|
+
if (typeof value === 'string' && RGB.test(value)) next.set(variable, value)
|
|
55
|
+
}
|
|
56
|
+
if (typeof theme.radius === 'string' && RADIUS.test(theme.radius)) {
|
|
57
|
+
next.set('--radius', theme.radius)
|
|
58
|
+
}
|
|
59
|
+
if (typeof theme.fontFamily === 'string' && FONT_STACK.test(theme.fontFamily)) {
|
|
60
|
+
next.set('--font-sans', theme.fontFamily)
|
|
61
|
+
}
|
|
62
|
+
for (const name of applied) {
|
|
63
|
+
if (!next.has(name)) root.style.removeProperty(name)
|
|
64
|
+
}
|
|
65
|
+
for (const [name, value] of next) root.style.setProperty(name, value)
|
|
66
|
+
applied = [...next.keys()]
|
|
67
|
+
const dark = theme.appearance === 'dark'
|
|
68
|
+
root.dataset.appearance = dark ? 'dark' : 'light'
|
|
69
|
+
root.style.colorScheme = dark ? 'dark' : 'light'
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// Ask for the current theme now that the listener is in place.
|
|
73
|
+
window.parent.postMessage({ type: 'mp:ready' }, '*')
|
|
74
|
+
}
|
|
@@ -1,33 +1,59 @@
|
|
|
1
1
|
:root {
|
|
2
2
|
color-scheme: light dark;
|
|
3
|
-
|
|
3
|
+
/* Theme tokens. Inside the customer portal, portal-theme.ts replaces them
|
|
4
|
+
with the portal's own colours, radius and font (light or dark). Style with
|
|
5
|
+
these variables, never fixed colours, so the app follows the portal. */
|
|
6
|
+
--background: #ffffff;
|
|
7
|
+
--foreground: #18181b;
|
|
8
|
+
--card: #ffffff;
|
|
9
|
+
--primary: #18181b;
|
|
10
|
+
--primary-foreground: #fafafa;
|
|
11
|
+
--muted: #f4f4f5;
|
|
12
|
+
--muted-foreground: #52525b;
|
|
13
|
+
--destructive: #b91c1c;
|
|
14
|
+
--border: #e4e4e7;
|
|
15
|
+
--ring: #18181b;
|
|
16
|
+
--radius: 0.5rem;
|
|
17
|
+
--font-sans:
|
|
4
18
|
ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
|
5
|
-
line-height: 1.5;
|
|
6
|
-
--border: #d4d4d8;
|
|
7
|
-
--muted: #52525b;
|
|
8
19
|
--notice: #fef3c7;
|
|
9
20
|
--notice-text: #78350f;
|
|
10
|
-
|
|
21
|
+
font-family: var(--font-sans);
|
|
22
|
+
line-height: 1.5;
|
|
11
23
|
}
|
|
12
24
|
|
|
13
25
|
@media (prefers-color-scheme: dark) {
|
|
14
|
-
:root {
|
|
26
|
+
:root:not([data-appearance='light']) {
|
|
27
|
+
--background: #09090b;
|
|
28
|
+
--foreground: #fafafa;
|
|
29
|
+
--card: #09090b;
|
|
30
|
+
--primary: #fafafa;
|
|
31
|
+
--primary-foreground: #18181b;
|
|
32
|
+
--muted: #27272a;
|
|
33
|
+
--muted-foreground: #a1a1aa;
|
|
34
|
+
--destructive: #fca5a5;
|
|
15
35
|
--border: #3f3f46;
|
|
16
|
-
--
|
|
36
|
+
--ring: #d4d4d8;
|
|
17
37
|
--notice: #422006;
|
|
18
38
|
--notice-text: #fde68a;
|
|
19
|
-
--error: #fca5a5;
|
|
20
39
|
}
|
|
21
40
|
}
|
|
22
41
|
|
|
42
|
+
:root[data-appearance='dark'] {
|
|
43
|
+
--notice: #422006;
|
|
44
|
+
--notice-text: #fde68a;
|
|
45
|
+
}
|
|
46
|
+
|
|
23
47
|
body {
|
|
24
48
|
margin: 0;
|
|
49
|
+
background: var(--background);
|
|
50
|
+
color: var(--foreground);
|
|
25
51
|
}
|
|
26
52
|
|
|
27
53
|
main {
|
|
28
54
|
max-width: 42rem;
|
|
29
55
|
margin: 0 auto;
|
|
30
|
-
padding:
|
|
56
|
+
padding: 2rem 1.5rem 3rem;
|
|
31
57
|
}
|
|
32
58
|
|
|
33
59
|
h1 {
|
|
@@ -57,10 +83,38 @@ textarea {
|
|
|
57
83
|
}
|
|
58
84
|
|
|
59
85
|
button {
|
|
60
|
-
padding: 0.
|
|
86
|
+
padding: 0.375rem 0.875rem;
|
|
87
|
+
border: 1px solid var(--border);
|
|
88
|
+
border-radius: var(--radius);
|
|
89
|
+
background: var(--card);
|
|
90
|
+
color: var(--foreground);
|
|
61
91
|
cursor: pointer;
|
|
62
92
|
}
|
|
63
93
|
|
|
94
|
+
button[type='submit'] {
|
|
95
|
+
background: var(--primary);
|
|
96
|
+
border-color: var(--primary);
|
|
97
|
+
color: var(--primary-foreground);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
input,
|
|
101
|
+
select,
|
|
102
|
+
textarea {
|
|
103
|
+
padding: 0.375rem 0.625rem;
|
|
104
|
+
border: 1px solid var(--border);
|
|
105
|
+
border-radius: var(--radius);
|
|
106
|
+
background: var(--background);
|
|
107
|
+
color: var(--foreground);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
button:focus-visible,
|
|
111
|
+
input:focus-visible,
|
|
112
|
+
select:focus-visible,
|
|
113
|
+
textarea:focus-visible {
|
|
114
|
+
outline: 2px solid var(--ring);
|
|
115
|
+
outline-offset: 1px;
|
|
116
|
+
}
|
|
117
|
+
|
|
64
118
|
button:disabled {
|
|
65
119
|
cursor: default;
|
|
66
120
|
}
|
|
@@ -78,18 +132,18 @@ label > textarea {
|
|
|
78
132
|
}
|
|
79
133
|
|
|
80
134
|
.muted {
|
|
81
|
-
color: var(--muted);
|
|
135
|
+
color: var(--muted-foreground);
|
|
82
136
|
}
|
|
83
137
|
|
|
84
138
|
.error {
|
|
85
|
-
color: var(--
|
|
139
|
+
color: var(--destructive);
|
|
86
140
|
}
|
|
87
141
|
|
|
88
142
|
.notice {
|
|
89
143
|
background: var(--notice);
|
|
90
144
|
color: var(--notice-text);
|
|
91
145
|
padding: 0.5rem 0.75rem;
|
|
92
|
-
border-radius:
|
|
146
|
+
border-radius: var(--radius);
|
|
93
147
|
}
|
|
94
148
|
|
|
95
149
|
.rows {
|
|
@@ -37,6 +37,12 @@ changes are simulated.
|
|
|
37
37
|
3. Test sign-in flows on the deployed app: locally nobody is signed in, so
|
|
38
38
|
`/api/me` answers 401.
|
|
39
39
|
|
|
40
|
+
The app follows the portal's look on its own: `src/ui/portal-theme.ts` receives
|
|
41
|
+
the portal's colours, radius and font (light or dark) and `src/ui/styles.css`
|
|
42
|
+
reads them as CSS variables. Style with those variables, not fixed colours, and
|
|
43
|
+
leave the screen without an outer card or border: the portal shows the app full
|
|
44
|
+
width, with no frame around it.
|
|
45
|
+
|
|
40
46
|
## Develop and publish
|
|
41
47
|
|
|
42
48
|
Requires [Bun](https://bun.sh).
|
|
@@ -38,6 +38,12 @@ the owner previews the tool, a banner says that changes are simulated.
|
|
|
38
38
|
3. Test it on the deployed app: locally nobody is signed in, so `/api/me`
|
|
39
39
|
answers 401.
|
|
40
40
|
|
|
41
|
+
`src/ui/styles.css` styles the app with theme variables. Team members open
|
|
42
|
+
this tool from the Marcapony dashboard, which sends no theme, so the defaults
|
|
43
|
+
in `styles.css` apply (light or dark from the device). If you also show the app
|
|
44
|
+
in the customer portal, `src/ui/portal-theme.ts` picks up the portal's colours
|
|
45
|
+
there. Style with the variables, not fixed colours.
|
|
46
|
+
|
|
41
47
|
## Develop and publish
|
|
42
48
|
|
|
43
49
|
Requires [Bun](https://bun.sh).
|