@shware/workflow-ui 2.0.0 → 2.0.1

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": "@shware/workflow-ui",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ShwareHQ/shware-sdk.git"
@@ -1,4 +1,12 @@
1
- import { type ReactNode, createContext, useCallback, useContext, useEffect, useState } from 'react';
1
+ import {
2
+ type ReactNode,
3
+ createContext,
4
+ useCallback,
5
+ useContext,
6
+ useEffect,
7
+ useState,
8
+ useSyncExternalStore,
9
+ } from 'react';
2
10
 
3
11
  /**
4
12
  * Light / dark / follow-the-OS, applied as a `dark` class on <html>.
@@ -21,6 +29,16 @@ function readStored(): Theme {
21
29
  const prefersDark = () =>
22
30
  typeof matchMedia === 'undefined' ? false : matchMedia('(prefers-color-scheme: dark)').matches;
23
31
 
32
+ function subscribeSystemTheme(onChange: () => void) {
33
+ if (typeof matchMedia === 'undefined') return () => {};
34
+ const query = matchMedia('(prefers-color-scheme: dark)');
35
+ query.addEventListener('change', onChange);
36
+ return () => query.removeEventListener('change', onChange);
37
+ }
38
+
39
+ const subscribeExplicitTheme = () => () => {};
40
+ const getServerSystemTheme = () => false;
41
+
24
42
  /** Runs before React mounts, so the first paint is already the right theme. */
25
43
  export function applyStoredTheme() {
26
44
  const theme = readStored();
@@ -42,17 +60,11 @@ const ThemeContext = createContext<ThemeContextValue | undefined>(undefined);
42
60
 
43
61
  export function ThemeProvider({ children }: { children: ReactNode }) {
44
62
  const [theme, setThemeState] = useState<Theme>(readStored);
45
- const [systemDark, setSystemDark] = useState(prefersDark);
46
-
47
- /* Track the OS only while `system` is selected; an explicit choice ignores it. */
48
- useEffect(() => {
49
- if (theme !== 'system' || typeof matchMedia === 'undefined') return;
50
- const query = matchMedia('(prefers-color-scheme: dark)');
51
- const onChange = (event: MediaQueryListEvent) => setSystemDark(event.matches);
52
- query.addEventListener('change', onChange);
53
- setSystemDark(query.matches);
54
- return () => query.removeEventListener('change', onChange);
55
- }, [theme]);
63
+ const systemDark = useSyncExternalStore(
64
+ theme === 'system' ? subscribeSystemTheme : subscribeExplicitTheme,
65
+ prefersDark,
66
+ getServerSystemTheme
67
+ );
56
68
 
57
69
  const resolved = theme === 'system' ? (systemDark ? 'dark' : 'light') : theme;
58
70
 
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  FloatingFocusManager,
3
+ type ReferenceType,
3
4
  autoUpdate,
4
5
  flip,
5
6
  offset,
@@ -90,6 +91,13 @@ export function DateRangePicker({ value, onChange, className }: Props) {
90
91
  whileElementsMounted: autoUpdate,
91
92
  middleware: [offset(4), flip({ padding: 8 }), shift({ padding: 8 })],
92
93
  });
94
+ const {
95
+ setFloating,
96
+ setReference,
97
+ }: {
98
+ setFloating: (this: void, node: HTMLElement | null) => void;
99
+ setReference: (this: void, node: ReferenceType | null) => void;
100
+ } = refs;
93
101
 
94
102
  const { getReferenceProps, getFloatingProps } = useInteractions([
95
103
  useClick(context),
@@ -116,7 +124,7 @@ export function DateRangePicker({ value, onChange, className }: Props) {
116
124
  return (
117
125
  <>
118
126
  <Button
119
- ref={refs.setReference}
127
+ ref={setReference}
120
128
  size="sm"
121
129
  variant="outline"
122
130
  className={cn('gap-2 font-normal', className)}
@@ -130,7 +138,7 @@ export function DateRangePicker({ value, onChange, className }: Props) {
130
138
  {open && (
131
139
  <FloatingFocusManager context={context} modal={false}>
132
140
  <div
133
- ref={refs.setFloating}
141
+ ref={setFloating}
134
142
  style={{ ...floatingStyles, ...superellipse }}
135
143
  className="border-border bg-card shadow-card-shadow z-50 w-64 rounded-xl border p-2 shadow-lg"
136
144
  {...getFloatingProps()}
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  FloatingFocusManager,
3
+ type ReferenceType,
3
4
  autoUpdate,
4
5
  flip,
5
6
  offset,
@@ -63,6 +64,13 @@ export function Dropdown({ value, options, onChange, placeholder, className }: P
63
64
  }),
64
65
  ],
65
66
  });
67
+ const {
68
+ setFloating,
69
+ setReference,
70
+ }: {
71
+ setFloating: (this: void, node: HTMLElement | null) => void;
72
+ setReference: (this: void, node: ReferenceType | null) => void;
73
+ } = refs;
66
74
 
67
75
  const listRef = useRef<(HTMLElement | null)[]>([]);
68
76
  const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([
@@ -78,7 +86,7 @@ export function Dropdown({ value, options, onChange, placeholder, className }: P
78
86
  return (
79
87
  <>
80
88
  <Button
81
- ref={refs.setReference}
89
+ ref={setReference}
82
90
  size="sm"
83
91
  variant="outline"
84
92
  className={cn(
@@ -106,7 +114,7 @@ export function Dropdown({ value, options, onChange, placeholder, className }: P
106
114
  {open && (
107
115
  <FloatingFocusManager context={context} modal={false}>
108
116
  <div
109
- ref={refs.setFloating}
117
+ ref={setFloating}
110
118
  style={floatingStyles}
111
119
  className={cn(
112
120
  'border-border bg-card z-50 max-w-96 min-w-64 overflow-y-auto rounded-xl border p-1',
@@ -19,22 +19,25 @@ export interface FlagProps {
19
19
  }
20
20
 
21
21
  export function Flag({ code, className }: FlagProps) {
22
- const [Component, setComponent] = useState<ComponentType<SVGProps<SVGSVGElement>> | undefined>(
23
- undefined
24
- );
25
- const load = FLAGS[`../app/assets/flags/${code.toUpperCase()}.svg`];
22
+ const key = `../app/assets/flags/${code.toUpperCase()}.svg`;
23
+ const load = Object.hasOwn(FLAGS, key) ? FLAGS[key] : undefined;
24
+ const [loaded, setLoaded] = useState<{
25
+ load: typeof load;
26
+ Component: ComponentType<SVGProps<SVGSVGElement>>;
27
+ }>();
26
28
 
27
29
  useEffect(() => {
30
+ if (load === undefined) return;
28
31
  let alive = true;
29
- setComponent(undefined);
30
- void load?.().then((mod) => {
31
- if (alive) setComponent(() => mod);
32
+ void load().then((Component) => {
33
+ if (alive) setLoaded({ load, Component });
32
34
  });
33
35
  return () => {
34
36
  alive = false;
35
37
  };
36
38
  }, [load]);
37
39
 
40
+ const Component = loaded !== undefined && loaded.load === load ? loaded.Component : undefined;
38
41
  if (Component === undefined) return null;
39
42
  return <Component className={className} aria-hidden />;
40
43
  }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  FloatingFocusManager,
3
+ type ReferenceType,
3
4
  autoUpdate,
4
5
  flip,
5
6
  offset,
@@ -47,6 +48,13 @@ export function Menu({ items, className, ...aria }: Props) {
47
48
  whileElementsMounted: autoUpdate,
48
49
  middleware: [offset(4), flip({ padding: 8 }), shift({ padding: 8 })],
49
50
  });
51
+ const {
52
+ setFloating,
53
+ setReference,
54
+ }: {
55
+ setFloating: (this: void, node: HTMLElement | null) => void;
56
+ setReference: (this: void, node: ReferenceType | null) => void;
57
+ } = refs;
50
58
 
51
59
  const listRef = useRef<(HTMLElement | null)[]>([]);
52
60
  const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([
@@ -60,7 +68,7 @@ export function Menu({ items, className, ...aria }: Props) {
60
68
  <>
61
69
  <button
62
70
  type="button"
63
- ref={refs.setReference}
71
+ ref={setReference}
64
72
  className={cn(
65
73
  'text-muted hover:bg-hover hover:text-primary flex size-8 items-center justify-center',
66
74
  'rounded-lg transition-colors focus:outline-none',
@@ -79,7 +87,7 @@ export function Menu({ items, className, ...aria }: Props) {
79
87
  {open && (
80
88
  <FloatingFocusManager context={context} modal={false}>
81
89
  <div
82
- ref={refs.setFloating}
90
+ ref={setFloating}
83
91
  style={floatingStyles}
84
92
  className="border-border bg-card z-50 min-w-36 rounded-xl border p-1 shadow-lg"
85
93
  {...getFloatingProps()}
@@ -32,6 +32,7 @@ export function Modal({ visible = false, className, disabled = false, children,
32
32
  if (!open && !disabled) onCancel?.();
33
33
  },
34
34
  });
35
+ const { setFloating }: { setFloating: (this: void, node: HTMLElement | null) => void } = refs;
35
36
 
36
37
  const { getFloatingProps } = useInteractions([
37
38
  useDismiss(context, { enabled: !disabled }),
@@ -48,7 +49,7 @@ export function Modal({ visible = false, className, disabled = false, children,
48
49
  >
49
50
  <FloatingFocusManager context={context}>
50
51
  <div
51
- ref={refs.setFloating}
52
+ ref={setFloating}
52
53
  style={superellipse}
53
54
  className={cn(
54
55
  'bg-card relative max-h-dvh max-w-dvw overflow-y-auto rounded-3xl',
@@ -118,7 +118,34 @@ function Row({
118
118
  );
119
119
  }
120
120
 
121
- /** Copy-to-clipboard tail for identity rows, flipping to a check as feedback. */
121
+ async function copyText(value: string): Promise<boolean> {
122
+ try {
123
+ await navigator.clipboard.writeText(value);
124
+ return true;
125
+ } catch {
126
+ // Clipboard permissions and embedded webviews can require the legacy fallback.
127
+ }
128
+
129
+ const area = document.createElement('textarea');
130
+ const previousFocus = document.activeElement;
131
+ area.value = value;
132
+ area.readOnly = true;
133
+ area.style.position = 'fixed';
134
+ area.style.opacity = '0';
135
+ try {
136
+ document.body.append(area);
137
+ area.select();
138
+ // oxlint-disable-next-line typescript/no-deprecated -- Only used when the Clipboard API is unavailable or rejects permission.
139
+ return document.execCommand('copy');
140
+ } catch {
141
+ return false;
142
+ } finally {
143
+ area.remove();
144
+ if (previousFocus instanceof HTMLElement) previousFocus.focus();
145
+ }
146
+ }
147
+
148
+ /** Copy-to-clipboard tail for identity rows, flipping to a check only after success. */
122
149
  function CopyButton({ label, value }: { label: string; value: string }) {
123
150
  const [copied, setCopied] = useState(false);
124
151
  useEffect(() => {
@@ -132,20 +159,7 @@ function CopyButton({ label, value }: { label: string; value: string }) {
132
159
  aria-label={label}
133
160
  title={label}
134
161
  onClick={() => {
135
- void (async () => {
136
- try {
137
- await navigator.clipboard.writeText(value);
138
- } catch {
139
- /* Clipboard API can be permission-gated (embedded webviews); fall back. */
140
- const area = document.createElement('textarea');
141
- area.value = value;
142
- document.body.append(area);
143
- area.select();
144
- document.execCommand('copy');
145
- area.remove();
146
- }
147
- setCopied(true);
148
- })();
162
+ void copyText(value).then(setCopied);
149
163
  }}
150
164
  className="text-muted hover:bg-hover hover:text-primary flex size-6 shrink-0 items-center justify-center rounded-md transition-colors"
151
165
  style={superellipse}
@@ -36,38 +36,39 @@ const textOf = (value: unknown): TextObject | undefined => {
36
36
  return typeof object.text === 'string' ? (object as TextObject) : undefined;
37
37
  };
38
38
 
39
+ const linkClass = 'text-[#1d9bd1] hover:underline';
40
+ const Text = ({ value, className }: { value: TextObject | undefined; className?: string }) =>
41
+ value === undefined ? null : (
42
+ <div className={`break-words whitespace-pre-wrap ${className ?? ''}`}>
43
+ {value.type === 'mrkdwn' ? (
44
+ <ChatMarkup text={value.text} dialect="slack" linkClass={linkClass} />
45
+ ) : (
46
+ value.text
47
+ )}
48
+ </div>
49
+ );
50
+ const Button = ({ element, colors }: { element: Element; colors: (typeof PALETTE)['light'] }) => {
51
+ const style = element.style as string | undefined;
52
+ return (
53
+ <span
54
+ className="inline-flex h-8 items-center rounded-lg border px-3 text-[13px] font-bold"
55
+ style={
56
+ style === 'primary'
57
+ ? { background: '#007a5a', borderColor: '#007a5a', color: '#fff' }
58
+ : style === 'danger'
59
+ ? { background: '#e01e5a', borderColor: '#e01e5a', color: '#fff' }
60
+ : { borderColor: colors.border, color: colors.name }
61
+ }
62
+ >
63
+ {textOf(element.text)?.text}
64
+ </span>
65
+ );
66
+ };
67
+
39
68
  export function SlackPreview({ message, appName, scheme, zoom }: SlackPreviewProps) {
40
69
  const { t } = useTranslation();
41
70
  const colors = PALETTE[scheme];
42
71
  const name = message.username ?? appName;
43
- const linkClass = 'text-[#1d9bd1] hover:underline';
44
- const Text = ({ value, className }: { value: TextObject | undefined; className?: string }) =>
45
- value === undefined ? null : (
46
- <div className={`break-words whitespace-pre-wrap ${className ?? ''}`}>
47
- {value.type === 'mrkdwn' ? (
48
- <ChatMarkup text={value.text} dialect="slack" linkClass={linkClass} />
49
- ) : (
50
- value.text
51
- )}
52
- </div>
53
- );
54
- const Button = ({ element }: { element: Element }) => {
55
- const style = element.style as string | undefined;
56
- return (
57
- <span
58
- className="inline-flex h-8 items-center rounded-lg border px-3 text-[13px] font-bold"
59
- style={
60
- style === 'primary'
61
- ? { background: '#007a5a', borderColor: '#007a5a', color: '#fff' }
62
- : style === 'danger'
63
- ? { background: '#e01e5a', borderColor: '#e01e5a', color: '#fff' }
64
- : { borderColor: colors.border, color: colors.name }
65
- }
66
- >
67
- {textOf(element.text)?.text}
68
- </span>
69
- );
70
- };
71
72
 
72
73
  const blocks = (message.blocks ?? []) as Element[];
73
74
  return (
@@ -154,7 +155,9 @@ export function SlackPreview({ message, appName, scheme, zoom }: SlackPreviewPro
154
155
  className="size-[88px] shrink-0 rounded-lg object-cover"
155
156
  />
156
157
  )}
157
- {accessory?.type === 'button' && <Button element={accessory} />}
158
+ {accessory?.type === 'button' && (
159
+ <Button element={accessory} colors={colors} />
160
+ )}
158
161
  </div>
159
162
  );
160
163
  }
@@ -187,7 +190,7 @@ export function SlackPreview({ message, appName, scheme, zoom }: SlackPreviewPro
187
190
  {((block.elements as Element[] | undefined) ?? []).map((element, i) =>
188
191
  element.type === 'button' ? (
189
192
  // oxlint-disable-next-line react/no-array-index-key -- elements are positional
190
- <Button key={i} element={element} />
193
+ <Button key={i} element={element} colors={colors} />
191
194
  ) : null
192
195
  )}
193
196
  </div>