@cxxi-lab/hubbyware-ui 0.6.0 → 0.6.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/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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cxxi-lab/hubbyware-ui",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Hubbyware's design system: React components, design tokens and fonts (formerly @cxxi-lab/hub-ui).",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,89 +1,20 @@
1
- import { useState, type ComponentPropsWithoutRef, type ReactNode } from 'react'
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, as the design system renders it everywhere (an AI's answers, a hubby's notes, the
8
- * back-office, the showcase, the docs). Safe by default — its text may come from a model or a user:
9
- * no raw HTML is ever rendered (shown as text), links open outside (and `javascript:` ones are
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 interface MarkdownProps {
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 PLAIN = components(false)
79
- const WITH_IMAGES = components(true)
12
+ const View = lazy(() => import('./MarkdownView').then(m => ({ default: m.MarkdownView })))
80
13
 
81
- export function Markdown({ children, images = false, className }: MarkdownProps) {
14
+ export function Markdown(props: MarkdownProps) {
82
15
  return (
83
- <div className={twMerge('text-sm leading-relaxed text-steel-100 break-words', className)}>
84
- <ReactMarkdown remarkPlugins={[remarkGfm]} components={images ? WITH_IMAGES : PLAIN} skipHtml={false}>
85
- {children}
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
+ }