@cxxi-lab/hubbyware-ui 0.6.0 → 0.7.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
CHANGED
|
@@ -12,5 +12,6 @@ back-office, the showcase, the docs): CommonMark and GitHub's extensions (tables
|
|
|
12
12
|
strikethrough, autolinks), code blocks with a copy button, styled with the design tokens. Safe by
|
|
13
13
|
default — the text may come from a model or a user: raw HTML is shown as text, never rendered;
|
|
14
14
|
`javascript:` links are dropped and links open outside; images are shown as links unless `images`.
|
|
15
|
-
It re-renders on each chunk of a stream (an unclosed code block already shows as code).
|
|
15
|
+
It re-renders on each chunk of a stream (an unclosed code block already shows as code). Its parser
|
|
16
|
+
(~100 kB gzipped) loads on first use: an app or a frame that never shows Markdown never loads it.
|
|
16
17
|
`npm test` checks all this.
|
package/package.json
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A code typed digit by digit (a PIN, a 2FA code): one box per digit, as a phone's code screens
|
|
5
|
+
* show it. One real input under the boxes, see-through: a phone opens its numeric keypad, a pasted
|
|
6
|
+
* or auto-filled code (`one-time-code`) lands whole, backspace and arrows behave as in a field.
|
|
7
|
+
* `masked`: dots instead of the digits (a PIN). `onComplete` when the last digit is typed.
|
|
8
|
+
*/
|
|
9
|
+
export function CodeInput({ length = 6, value, onChange, onComplete, masked = false, autoFocus = false, isDisabled = false, isInvalid = false, label }: {
|
|
10
|
+
length?: number
|
|
11
|
+
value: string
|
|
12
|
+
onChange: (value: string) => void
|
|
13
|
+
onComplete?: (value: string) => void
|
|
14
|
+
masked?: boolean
|
|
15
|
+
autoFocus?: boolean
|
|
16
|
+
isDisabled?: boolean
|
|
17
|
+
/** the boxes in red (a wrong code) */
|
|
18
|
+
isInvalid?: boolean
|
|
19
|
+
/** said to screen readers */
|
|
20
|
+
label: string
|
|
21
|
+
}) {
|
|
22
|
+
const input = useRef<HTMLInputElement>(null)
|
|
23
|
+
const [focused, setFocused] = useState(false)
|
|
24
|
+
useEffect(() => { if (autoFocus) input.current?.focus() }, [autoFocus])
|
|
25
|
+
|
|
26
|
+
const change = (raw: string) => {
|
|
27
|
+
const digits = raw.replace(/\D/g, '').slice(0, length)
|
|
28
|
+
onChange(digits)
|
|
29
|
+
if (digits.length === length) onComplete?.(digits)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<div className="relative inline-flex gap-2" onClick={() => input.current?.focus()}>
|
|
34
|
+
{Array.from({ length }, (_, i) => {
|
|
35
|
+
const digit = value[i]
|
|
36
|
+
const current = focused && (i === value.length || (i === length - 1 && value.length === length))
|
|
37
|
+
return (
|
|
38
|
+
<div
|
|
39
|
+
key={i}
|
|
40
|
+
aria-hidden
|
|
41
|
+
className={[
|
|
42
|
+
'flex items-center justify-center w-11 h-13 max-sm:w-10 max-sm:h-12 rounded border text-xl font-semibold text-white bg-steel-950 transition-colors',
|
|
43
|
+
isInvalid ? 'border-red-500' : current ? 'border-primary-500' : digit !== undefined ? 'border-steel-300' : 'border-steel-700'
|
|
44
|
+
].join(' ')}
|
|
45
|
+
>
|
|
46
|
+
{digit === undefined ? '' : masked ? <span className="size-2.5 rounded-full bg-white" /> : digit}
|
|
47
|
+
</div>
|
|
48
|
+
)
|
|
49
|
+
})}
|
|
50
|
+
<input
|
|
51
|
+
ref={input}
|
|
52
|
+
aria-label={label}
|
|
53
|
+
type="text"
|
|
54
|
+
inputMode="numeric"
|
|
55
|
+
pattern="[0-9]*"
|
|
56
|
+
autoComplete="one-time-code"
|
|
57
|
+
maxLength={length}
|
|
58
|
+
value={value}
|
|
59
|
+
disabled={isDisabled}
|
|
60
|
+
onChange={e => change(e.target.value)}
|
|
61
|
+
onFocus={() => setFocused(true)}
|
|
62
|
+
onBlur={() => setFocused(false)}
|
|
63
|
+
className="absolute inset-0 w-full h-full opacity-0 cursor-text caret-transparent text-transparent bg-transparent border-0 outline-none"
|
|
64
|
+
/>
|
|
65
|
+
</div>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CodeInput } from './CodeInput'
|
package/src/forms/index.ts
CHANGED
|
@@ -1,89 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import ReactMarkdown, { type Components } from 'react-markdown'
|
|
3
|
-
import remarkGfm from 'remark-gfm'
|
|
1
|
+
import { lazy, Suspense } from 'react'
|
|
4
2
|
import { twMerge } from 'tailwind-merge'
|
|
3
|
+
import type { MarkdownViewProps } from './MarkdownView'
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
|
-
* Markdown,
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* dropped), images are not loaded unless `images` allows them. CommonMark + GitHub's extensions
|
|
11
|
-
* (tables, task lists, strikethrough, autolinks); code blocks with a copy button. Re-rendered on each
|
|
12
|
-
* chunk of a stream without flicker (an unclosed code block already shows as code).
|
|
6
|
+
* Markdown, rendered the same everywhere (./MarkdownView.tsx: safe by default, GFM, code blocks).
|
|
7
|
+
* Its parser is loaded on first use only — a hubby's frame that never shows Markdown never loads it;
|
|
8
|
+
* until it is there (a moment, once), the text shows as it is.
|
|
13
9
|
*/
|
|
14
|
-
export
|
|
15
|
-
children: string
|
|
16
|
-
/** load the images it links to (default: shown as links) */
|
|
17
|
-
images?: boolean
|
|
18
|
-
className?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function CopyButton({ text }: { text: string }) {
|
|
22
|
-
const [copied, setCopied] = useState(false)
|
|
23
|
-
return (
|
|
24
|
-
<button
|
|
25
|
-
type="button"
|
|
26
|
-
onClick={() => void navigator.clipboard?.writeText(text).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1500) })}
|
|
27
|
-
className="absolute top-1.5 right-1.5 rounded px-1.5 py-0.5 text-[11px] text-steel-300 bg-steel-800/80 border border-steel-700 opacity-0 group-hover:opacity-100 focus:opacity-100 cursor-pointer"
|
|
28
|
-
>
|
|
29
|
-
{copied ? 'Copied' : 'Copy'}
|
|
30
|
-
</button>
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** The text of a rendered node (a code block's, for its copy button). */
|
|
35
|
-
function textOf(node: ReactNode): string {
|
|
36
|
-
if (typeof node === 'string' || typeof node === 'number') return String(node)
|
|
37
|
-
if (Array.isArray(node)) return node.map(textOf).join('')
|
|
38
|
-
if (node && typeof node === 'object' && 'props' in node) return textOf((node as { props: { children?: ReactNode } }).props.children)
|
|
39
|
-
return ''
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function components(images: boolean): Components {
|
|
43
|
-
return {
|
|
44
|
-
h1: ({ node: _n, ...p }) => <h1 className="mt-4 mb-2 text-xl font-semibold text-white first:mt-0" {...p} />,
|
|
45
|
-
h2: ({ node: _n, ...p }) => <h2 className="mt-4 mb-2 text-lg font-semibold text-white first:mt-0" {...p} />,
|
|
46
|
-
h3: ({ node: _n, ...p }) => <h3 className="mt-3 mb-1.5 text-base font-semibold text-white first:mt-0" {...p} />,
|
|
47
|
-
h4: ({ node: _n, ...p }) => <h4 className="mt-3 mb-1 text-sm font-semibold text-white first:mt-0" {...p} />,
|
|
48
|
-
p: ({ node: _n, ...p }) => <p className="my-2 first:mt-0 last:mb-0" {...p} />,
|
|
49
|
-
a: ({ node: _n, ...p }) => <a className="text-primary-400 underline underline-offset-2 hover:text-primary-300" target="_blank" rel="noopener noreferrer" {...p} />,
|
|
50
|
-
ul: ({ node: _n, className, ...p }) => <ul className={twMerge('my-2 pl-5 list-disc marker:text-steel-400', className?.includes('contains-task-list') && 'pl-1 list-none')} {...p} />,
|
|
51
|
-
ol: ({ node: _n, ...p }) => <ol className="my-2 pl-5 list-decimal marker:text-steel-400" {...p} />,
|
|
52
|
-
li: ({ node: _n, ...p }) => <li className="my-0.5" {...p} />,
|
|
53
|
-
input: ({ node: _n, ...p }) => <input {...p} disabled className="mr-1.5 align-middle accent-primary-500" />,
|
|
54
|
-
blockquote: ({ node: _n, ...p }) => <blockquote className="my-2 border-l-2 border-steel-600 pl-3 text-steel-200" {...p} />,
|
|
55
|
-
hr: () => <hr className="my-4 border-steel-700" />,
|
|
56
|
-
del: ({ node: _n, ...p }) => <del className="text-steel-400" {...p} />,
|
|
57
|
-
pre: ({ node: _n, children }) => (
|
|
58
|
-
<div className="group relative my-2">
|
|
59
|
-
<pre className="overflow-x-auto rounded-md bg-steel-950 border border-steel-800 p-3 text-[13px] leading-relaxed">{children}</pre>
|
|
60
|
-
<CopyButton text={textOf(children).replace(/\n$/, '')} />
|
|
61
|
-
</div>
|
|
62
|
-
),
|
|
63
|
-
code: ({ node: _n, className, ...p }: ComponentPropsWithoutRef<'code'> & { node?: unknown }) => (
|
|
64
|
-
// in a block (<pre>): its language class; inline: a chip
|
|
65
|
-
className?.startsWith('language-') || (p.children && String(p.children).includes('\n'))
|
|
66
|
-
? <code className={twMerge('font-mono text-steel-100', className)} {...p} />
|
|
67
|
-
: <code className="rounded bg-steel-800 px-1 py-0.5 font-mono text-[0.9em] text-steel-100" {...p} />
|
|
68
|
-
),
|
|
69
|
-
table: ({ node: _n, ...p }) => <div className="my-2 overflow-x-auto"><table className="border-collapse text-sm" {...p} /></div>,
|
|
70
|
-
th: ({ node: _n, ...p }) => <th className="border border-steel-700 bg-steel-800 px-2 py-1 text-left font-semibold text-white" {...p} />,
|
|
71
|
-
td: ({ node: _n, ...p }) => <td className="border border-steel-700 px-2 py-1" {...p} />,
|
|
72
|
-
img: ({ node: _n, src, alt }) => images && typeof src === 'string'
|
|
73
|
-
? <img src={src} alt={alt ?? ''} className="my-2 max-w-full rounded" loading="lazy" />
|
|
74
|
-
: <a href={typeof src === 'string' ? src : undefined} target="_blank" rel="noopener noreferrer" className="text-primary-400 underline">{alt || 'image'}</a>
|
|
75
|
-
}
|
|
76
|
-
}
|
|
10
|
+
export type MarkdownProps = MarkdownViewProps
|
|
77
11
|
|
|
78
|
-
const
|
|
79
|
-
const WITH_IMAGES = components(true)
|
|
12
|
+
const View = lazy(() => import('./MarkdownView').then(m => ({ default: m.MarkdownView })))
|
|
80
13
|
|
|
81
|
-
export function Markdown(
|
|
14
|
+
export function Markdown(props: MarkdownProps) {
|
|
82
15
|
return (
|
|
83
|
-
<div className={twMerge('text-sm leading-relaxed text-steel-100 break-words', className)}>
|
|
84
|
-
<
|
|
85
|
-
|
|
86
|
-
</ReactMarkdown>
|
|
87
|
-
</div>
|
|
16
|
+
<Suspense fallback={<div className={twMerge('text-sm leading-relaxed text-steel-100 break-words whitespace-pre-wrap', props.className)}>{props.children}</div>}>
|
|
17
|
+
<View {...props} />
|
|
18
|
+
</Suspense>
|
|
88
19
|
)
|
|
89
20
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { useState, type ComponentPropsWithoutRef, type ReactNode } from 'react'
|
|
2
|
+
import ReactMarkdown, { type Components } from 'react-markdown'
|
|
3
|
+
import remarkGfm from 'remark-gfm'
|
|
4
|
+
import { twMerge } from 'tailwind-merge'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The Markdown renderer itself (loaded on first use by Markdown, ./Markdown.tsx: its parser weighs
|
|
8
|
+
* ~100 kB gzipped, the frames that never show Markdown never load it). Markdown, as the design system renders it everywhere (an AI's answers, a hubby's notes, the
|
|
9
|
+
* back-office, the showcase, the docs). Safe by default — its text may come from a model or a user:
|
|
10
|
+
* no raw HTML is ever rendered (shown as text), links open outside (and `javascript:` ones are
|
|
11
|
+
* dropped), images are not loaded unless `images` allows them. CommonMark + GitHub's extensions
|
|
12
|
+
* (tables, task lists, strikethrough, autolinks); code blocks with a copy button. Re-rendered on each
|
|
13
|
+
* chunk of a stream without flicker (an unclosed code block already shows as code).
|
|
14
|
+
*/
|
|
15
|
+
export interface MarkdownViewProps {
|
|
16
|
+
children: string
|
|
17
|
+
/** load the images it links to (default: shown as links) */
|
|
18
|
+
images?: boolean
|
|
19
|
+
className?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function CopyButton({ text }: { text: string }) {
|
|
23
|
+
const [copied, setCopied] = useState(false)
|
|
24
|
+
return (
|
|
25
|
+
<button
|
|
26
|
+
type="button"
|
|
27
|
+
onClick={() => void navigator.clipboard?.writeText(text).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1500) })}
|
|
28
|
+
className="absolute top-1.5 right-1.5 rounded px-1.5 py-0.5 text-[11px] text-steel-300 bg-steel-800/80 border border-steel-700 opacity-0 group-hover:opacity-100 focus:opacity-100 cursor-pointer"
|
|
29
|
+
>
|
|
30
|
+
{copied ? 'Copied' : 'Copy'}
|
|
31
|
+
</button>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The text of a rendered node (a code block's, for its copy button). */
|
|
36
|
+
function textOf(node: ReactNode): string {
|
|
37
|
+
if (typeof node === 'string' || typeof node === 'number') return String(node)
|
|
38
|
+
if (Array.isArray(node)) return node.map(textOf).join('')
|
|
39
|
+
if (node && typeof node === 'object' && 'props' in node) return textOf((node as { props: { children?: ReactNode } }).props.children)
|
|
40
|
+
return ''
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function components(images: boolean): Components {
|
|
44
|
+
return {
|
|
45
|
+
h1: ({ node: _n, ...p }) => <h1 className="mt-4 mb-2 text-xl font-semibold text-white first:mt-0" {...p} />,
|
|
46
|
+
h2: ({ node: _n, ...p }) => <h2 className="mt-4 mb-2 text-lg font-semibold text-white first:mt-0" {...p} />,
|
|
47
|
+
h3: ({ node: _n, ...p }) => <h3 className="mt-3 mb-1.5 text-base font-semibold text-white first:mt-0" {...p} />,
|
|
48
|
+
h4: ({ node: _n, ...p }) => <h4 className="mt-3 mb-1 text-sm font-semibold text-white first:mt-0" {...p} />,
|
|
49
|
+
p: ({ node: _n, ...p }) => <p className="my-2 first:mt-0 last:mb-0" {...p} />,
|
|
50
|
+
a: ({ node: _n, ...p }) => <a className="text-primary-400 underline underline-offset-2 hover:text-primary-300" target="_blank" rel="noopener noreferrer" {...p} />,
|
|
51
|
+
ul: ({ node: _n, className, ...p }) => <ul className={twMerge('my-2 pl-5 list-disc marker:text-steel-400', className?.includes('contains-task-list') && 'pl-1 list-none')} {...p} />,
|
|
52
|
+
ol: ({ node: _n, ...p }) => <ol className="my-2 pl-5 list-decimal marker:text-steel-400" {...p} />,
|
|
53
|
+
li: ({ node: _n, ...p }) => <li className="my-0.5" {...p} />,
|
|
54
|
+
input: ({ node: _n, ...p }) => <input {...p} disabled className="mr-1.5 align-middle accent-primary-500" />,
|
|
55
|
+
blockquote: ({ node: _n, ...p }) => <blockquote className="my-2 border-l-2 border-steel-600 pl-3 text-steel-200" {...p} />,
|
|
56
|
+
hr: () => <hr className="my-4 border-steel-700" />,
|
|
57
|
+
del: ({ node: _n, ...p }) => <del className="text-steel-400" {...p} />,
|
|
58
|
+
pre: ({ node: _n, children }) => (
|
|
59
|
+
<div className="group relative my-2">
|
|
60
|
+
<pre className="overflow-x-auto rounded-md bg-steel-950 border border-steel-800 p-3 text-[13px] leading-relaxed">{children}</pre>
|
|
61
|
+
<CopyButton text={textOf(children).replace(/\n$/, '')} />
|
|
62
|
+
</div>
|
|
63
|
+
),
|
|
64
|
+
code: ({ node: _n, className, ...p }: ComponentPropsWithoutRef<'code'> & { node?: unknown }) => (
|
|
65
|
+
// in a block (<pre>): its language class; inline: a chip
|
|
66
|
+
className?.startsWith('language-') || (p.children && String(p.children).includes('\n'))
|
|
67
|
+
? <code className={twMerge('font-mono text-steel-100', className)} {...p} />
|
|
68
|
+
: <code className="rounded bg-steel-800 px-1 py-0.5 font-mono text-[0.9em] text-steel-100" {...p} />
|
|
69
|
+
),
|
|
70
|
+
table: ({ node: _n, ...p }) => <div className="my-2 overflow-x-auto"><table className="border-collapse text-sm" {...p} /></div>,
|
|
71
|
+
th: ({ node: _n, ...p }) => <th className="border border-steel-700 bg-steel-800 px-2 py-1 text-left font-semibold text-white" {...p} />,
|
|
72
|
+
td: ({ node: _n, ...p }) => <td className="border border-steel-700 px-2 py-1" {...p} />,
|
|
73
|
+
img: ({ node: _n, src, alt }) => images && typeof src === 'string'
|
|
74
|
+
? <img src={src} alt={alt ?? ''} className="my-2 max-w-full rounded" loading="lazy" />
|
|
75
|
+
: <a href={typeof src === 'string' ? src : undefined} target="_blank" rel="noopener noreferrer" className="text-primary-400 underline">{alt || 'image'}</a>
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const PLAIN = components(false)
|
|
80
|
+
const WITH_IMAGES = components(true)
|
|
81
|
+
|
|
82
|
+
export function MarkdownView({ children, images = false, className }: MarkdownViewProps) {
|
|
83
|
+
return (
|
|
84
|
+
<div className={twMerge('text-sm leading-relaxed text-steel-100 break-words', className)}>
|
|
85
|
+
<ReactMarkdown remarkPlugins={[remarkGfm]} components={images ? WITH_IMAGES : PLAIN} skipHtml={false}>
|
|
86
|
+
{children}
|
|
87
|
+
</ReactMarkdown>
|
|
88
|
+
</div>
|
|
89
|
+
)
|
|
90
|
+
}
|