@kushagradhawan/kookie-blocks 0.1.6 → 0.1.8
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/components.css +67 -64
- package/dist/cjs/components/code/CodeBlock.d.ts +2 -1
- package/dist/cjs/components/code/CodeBlock.d.ts.map +1 -1
- package/dist/cjs/components/code/CodeBlock.js +1 -1
- package/dist/cjs/components/code/CodeBlock.js.map +3 -3
- package/dist/cjs/components/code/LanguageBadge.d.ts +9 -0
- package/dist/cjs/components/code/LanguageBadge.d.ts.map +1 -1
- package/dist/cjs/components/code/LanguageBadge.js +1 -1
- package/dist/cjs/components/code/LanguageBadge.js.map +3 -3
- package/dist/cjs/components/code/index.d.ts +5 -2
- package/dist/cjs/components/code/index.d.ts.map +1 -1
- package/dist/cjs/components/code/index.js +1 -1
- package/dist/cjs/components/code/index.js.map +3 -3
- package/dist/cjs/components/code/types.d.ts +132 -13
- package/dist/cjs/components/code/types.d.ts.map +1 -1
- package/dist/cjs/components/code/types.js +1 -1
- package/dist/cjs/components/code/types.js.map +3 -3
- package/dist/cjs/components/code/useCodeCard.d.ts +20 -0
- package/dist/cjs/components/code/useCodeCard.d.ts.map +1 -0
- package/dist/cjs/components/code/useCodeCard.js +2 -0
- package/dist/cjs/components/code/useCodeCard.js.map +7 -0
- package/dist/esm/components/code/CodeBlock.d.ts +2 -1
- package/dist/esm/components/code/CodeBlock.d.ts.map +1 -1
- package/dist/esm/components/code/CodeBlock.js +1 -1
- package/dist/esm/components/code/CodeBlock.js.map +3 -3
- package/dist/esm/components/code/LanguageBadge.d.ts +9 -0
- package/dist/esm/components/code/LanguageBadge.d.ts.map +1 -1
- package/dist/esm/components/code/LanguageBadge.js +1 -1
- package/dist/esm/components/code/LanguageBadge.js.map +3 -3
- package/dist/esm/components/code/index.d.ts +5 -2
- package/dist/esm/components/code/index.d.ts.map +1 -1
- package/dist/esm/components/code/index.js +1 -1
- package/dist/esm/components/code/index.js.map +3 -3
- package/dist/esm/components/code/types.d.ts +132 -13
- package/dist/esm/components/code/types.d.ts.map +1 -1
- package/dist/esm/components/code/types.js +1 -0
- package/dist/esm/components/code/types.js.map +4 -4
- package/dist/esm/components/code/useCodeCard.d.ts +20 -0
- package/dist/esm/components/code/useCodeCard.d.ts.map +1 -0
- package/dist/esm/components/code/useCodeCard.js +2 -0
- package/dist/esm/components/code/useCodeCard.js.map +7 -0
- package/package.json +1 -1
- package/src/components/code/CodeBlock.tsx +252 -330
- package/src/components/code/LanguageBadge.tsx +65 -18
- package/src/components/code/index.ts +6 -3
- package/src/components/code/types.ts +219 -27
- package/src/components/code/useCodeCard.ts +82 -0
- package/src/components/index.css +64 -62
- package/styles.css +60 -59
|
@@ -1,24 +1,143 @@
|
|
|
1
|
-
import type { ReactNode } from "react";
|
|
1
|
+
import type { ReactNode, ReactElement } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Shiki syntax highlighting configuration.
|
|
4
|
+
* Controls themes and advanced highlighting options.
|
|
5
|
+
*/
|
|
6
|
+
export interface ShikiConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Theme names for light and dark modes.
|
|
9
|
+
* Uses Shiki's dual-theme mode for automatic theme switching.
|
|
10
|
+
* @default { light: 'one-light', dark: 'one-dark-pro' }
|
|
11
|
+
*/
|
|
12
|
+
themes?: {
|
|
13
|
+
light?: string;
|
|
14
|
+
dark?: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Language aliases for custom language mapping.
|
|
18
|
+
* @example { js: 'javascript', ts: 'typescript' }
|
|
19
|
+
*/
|
|
20
|
+
langAlias?: Record<string, string>;
|
|
21
|
+
/**
|
|
22
|
+
* Shiki transformers for custom code transformations.
|
|
23
|
+
* Useful for line highlighting, diff syntax, etc.
|
|
24
|
+
* @see https://shiki.style/guide/transformers
|
|
25
|
+
*/
|
|
26
|
+
transformers?: any[];
|
|
27
|
+
/**
|
|
28
|
+
* Metadata string passed to transformers.
|
|
29
|
+
* Often used for line highlighting syntax like `{1,3-5}`.
|
|
30
|
+
*/
|
|
31
|
+
meta?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Background configuration for preview section.
|
|
35
|
+
*/
|
|
36
|
+
export interface PreviewBackgroundProps {
|
|
37
|
+
/** Dot size in pixels (for dots background) */
|
|
38
|
+
dotSize?: number;
|
|
39
|
+
/** Dot color (CSS value) */
|
|
40
|
+
color?: string;
|
|
41
|
+
/** Background color (CSS value) */
|
|
42
|
+
backgroundColor?: string;
|
|
43
|
+
/** Preview section height */
|
|
44
|
+
height?: string;
|
|
45
|
+
/** Preview section width */
|
|
46
|
+
width?: string;
|
|
47
|
+
/** Border radius token (e.g., "3" for var(--radius-3)) */
|
|
48
|
+
radius?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* CodeBlock Component Props
|
|
52
|
+
*
|
|
53
|
+
* A unified code block component supporting:
|
|
54
|
+
* - Runtime syntax highlighting via Shiki (`code` + `language`)
|
|
55
|
+
* - Build-time highlighted content from MDX/rehype-pretty-code (`children`)
|
|
56
|
+
* - Optional preview section for documentation
|
|
57
|
+
*/
|
|
2
58
|
export interface CodeBlockProps {
|
|
3
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Raw code string for runtime Shiki highlighting.
|
|
61
|
+
* Mutually exclusive with `children`.
|
|
62
|
+
*/
|
|
4
63
|
code?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Language identifier for syntax highlighting.
|
|
66
|
+
* Required when using `code` prop.
|
|
67
|
+
* @example "typescript", "tsx", "python", "bash"
|
|
68
|
+
*/
|
|
5
69
|
language?: string;
|
|
6
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Pre-highlighted content from MDX/rehype-pretty-code.
|
|
72
|
+
* Mutually exclusive with `code` prop.
|
|
73
|
+
* Language is auto-detected from `data-language` or `className="language-xxx"`.
|
|
74
|
+
*/
|
|
75
|
+
children?: ReactNode;
|
|
76
|
+
/**
|
|
77
|
+
* Shiki syntax highlighting configuration.
|
|
78
|
+
* Only applies when using `code` prop (runtime highlighting).
|
|
79
|
+
*/
|
|
80
|
+
shikiConfig?: ShikiConfig;
|
|
81
|
+
/**
|
|
82
|
+
* Show copy-to-clipboard button.
|
|
83
|
+
* @default true
|
|
84
|
+
*/
|
|
7
85
|
showCopy?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Show language badge.
|
|
88
|
+
* @default true
|
|
89
|
+
*/
|
|
8
90
|
showLanguage?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Show line numbers.
|
|
93
|
+
* @default true
|
|
94
|
+
*/
|
|
95
|
+
showLineNumbers?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* File path displayed above the code block.
|
|
98
|
+
* @example "src/components/Button.tsx"
|
|
99
|
+
*/
|
|
9
100
|
file?: string;
|
|
101
|
+
/**
|
|
102
|
+
* Enable expand/collapse for long code blocks.
|
|
103
|
+
* @default true
|
|
104
|
+
*/
|
|
10
105
|
collapsible?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Height threshold (in pixels) before code is collapsed.
|
|
108
|
+
* @default 360
|
|
109
|
+
*/
|
|
11
110
|
collapsedHeight?: number;
|
|
111
|
+
/**
|
|
112
|
+
* Preview component displayed above the code block.
|
|
113
|
+
* Useful for showcasing component examples in documentation.
|
|
114
|
+
*/
|
|
115
|
+
preview?: ReactNode;
|
|
116
|
+
/**
|
|
117
|
+
* Background style for preview section.
|
|
118
|
+
* - `"none"`: Plain card background
|
|
119
|
+
* - `"dots"`: Dotted pattern background
|
|
120
|
+
* - `string`: Image URL for custom background
|
|
121
|
+
* @default "none"
|
|
122
|
+
*/
|
|
12
123
|
background?: "none" | "dots" | string;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
height?: string;
|
|
18
|
-
width?: string;
|
|
19
|
-
radius?: string;
|
|
20
|
-
};
|
|
21
|
-
lightTheme?: string;
|
|
22
|
-
darkTheme?: string;
|
|
124
|
+
/**
|
|
125
|
+
* Customization for preview background.
|
|
126
|
+
*/
|
|
127
|
+
backgroundProps?: PreviewBackgroundProps;
|
|
23
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Type guard for React elements with props.
|
|
131
|
+
*/
|
|
132
|
+
export declare function isReactElement(node: unknown): node is ReactElement;
|
|
133
|
+
/**
|
|
134
|
+
* Extracts plain text content from React children.
|
|
135
|
+
* Used to get copyable code from pre-highlighted MDX content.
|
|
136
|
+
*/
|
|
137
|
+
export declare function extractTextFromChildren(children: ReactNode): string;
|
|
138
|
+
/**
|
|
139
|
+
* Extracts language identifier from pre-highlighted MDX children.
|
|
140
|
+
* Checks `data-language` attribute and `className="language-xxx"` pattern.
|
|
141
|
+
*/
|
|
142
|
+
export declare function extractLanguageFromChildren(children: ReactNode): string;
|
|
24
143
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/components/code/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/components/code/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErD;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B;;;;OAIG;IACH,MAAM,CAAC,EAAE;QACP,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;IAEF;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEnC;;;;OAIG;IAEH,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC;IAErB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4BAA4B;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mCAAmC;IACnC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,6BAA6B;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4BAA4B;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,0DAA0D;IAC1D,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC;IAErB;;;OAGG;IACH,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,OAAO,CAAC,EAAE,SAAS,CAAC;IAEpB;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;IAEtC;;OAEG;IACH,eAAe,CAAC,EAAE,sBAAsB,CAAC;CAC1C;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,YAAY,CAElE;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,SAAS,GAAG,MAAM,CAiBnE;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,CAAC,QAAQ,EAAE,SAAS,GAAG,MAAM,CAyCvE"}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
+
function s(r){return typeof r=="object"&&r!==null&&"props"in r&&typeof r.props=="object"}function g(r){if(typeof r=="string")return r;if(typeof r=="number")return String(r);if(!r)return"";if(Array.isArray(r))return r.map(g).join("");if(s(r)){const t=r.props;if(t.children)return g(t.children)}return""}function c(r){const t=o=>{if(!o)return null;if(s(o)){const e=o.props;if(e["data-language"])return e["data-language"];const i=e.className||e.class||"";if(typeof i=="string"){const n=i.match(/language-([\w-]+)/i);if(n)return n[1]}if(e.children)if(Array.isArray(e.children))for(const n of e.children){const a=t(n);if(a)return a}else return t(e.children)}return null};return t(r)||"text"}export{c as extractLanguageFromChildren,g as extractTextFromChildren,s as isReactElement};
|
|
1
2
|
//# sourceMappingURL=types.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": [],
|
|
4
|
-
"sourcesContent": [],
|
|
5
|
-
"mappings": "",
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../../../../src/components/code/types.ts"],
|
|
4
|
+
"sourcesContent": ["import type { ReactNode, ReactElement } from \"react\";\n\n/**\n * Shiki syntax highlighting configuration.\n * Controls themes and advanced highlighting options.\n */\nexport interface ShikiConfig {\n /**\n * Theme names for light and dark modes.\n * Uses Shiki's dual-theme mode for automatic theme switching.\n * @default { light: 'one-light', dark: 'one-dark-pro' }\n */\n themes?: {\n light?: string;\n dark?: string;\n };\n\n /**\n * Language aliases for custom language mapping.\n * @example { js: 'javascript', ts: 'typescript' }\n */\n langAlias?: Record<string, string>;\n\n /**\n * Shiki transformers for custom code transformations.\n * Useful for line highlighting, diff syntax, etc.\n * @see https://shiki.style/guide/transformers\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n transformers?: any[];\n\n /**\n * Metadata string passed to transformers.\n * Often used for line highlighting syntax like `{1,3-5}`.\n */\n meta?: string;\n}\n\n/**\n * Background configuration for preview section.\n */\nexport interface PreviewBackgroundProps {\n /** Dot size in pixels (for dots background) */\n dotSize?: number;\n /** Dot color (CSS value) */\n color?: string;\n /** Background color (CSS value) */\n backgroundColor?: string;\n /** Preview section height */\n height?: string;\n /** Preview section width */\n width?: string;\n /** Border radius token (e.g., \"3\" for var(--radius-3)) */\n radius?: string;\n}\n\n/**\n * CodeBlock Component Props\n *\n * A unified code block component supporting:\n * - Runtime syntax highlighting via Shiki (`code` + `language`)\n * - Build-time highlighted content from MDX/rehype-pretty-code (`children`)\n * - Optional preview section for documentation\n */\nexport interface CodeBlockProps {\n /**\n * Raw code string for runtime Shiki highlighting.\n * Mutually exclusive with `children`.\n */\n code?: string;\n\n /**\n * Language identifier for syntax highlighting.\n * Required when using `code` prop.\n * @example \"typescript\", \"tsx\", \"python\", \"bash\"\n */\n language?: string;\n\n /**\n * Pre-highlighted content from MDX/rehype-pretty-code.\n * Mutually exclusive with `code` prop.\n * Language is auto-detected from `data-language` or `className=\"language-xxx\"`.\n */\n children?: ReactNode;\n\n /**\n * Shiki syntax highlighting configuration.\n * Only applies when using `code` prop (runtime highlighting).\n */\n shikiConfig?: ShikiConfig;\n\n /**\n * Show copy-to-clipboard button.\n * @default true\n */\n showCopy?: boolean;\n\n /**\n * Show language badge.\n * @default true\n */\n showLanguage?: boolean;\n\n /**\n * Show line numbers.\n * @default true\n */\n showLineNumbers?: boolean;\n\n /**\n * File path displayed above the code block.\n * @example \"src/components/Button.tsx\"\n */\n file?: string;\n\n /**\n * Enable expand/collapse for long code blocks.\n * @default true\n */\n collapsible?: boolean;\n\n /**\n * Height threshold (in pixels) before code is collapsed.\n * @default 360\n */\n collapsedHeight?: number;\n\n /**\n * Preview component displayed above the code block.\n * Useful for showcasing component examples in documentation.\n */\n preview?: ReactNode;\n\n /**\n * Background style for preview section.\n * - `\"none\"`: Plain card background\n * - `\"dots\"`: Dotted pattern background\n * - `string`: Image URL for custom background\n * @default \"none\"\n */\n background?: \"none\" | \"dots\" | string;\n\n /**\n * Customization for preview background.\n */\n backgroundProps?: PreviewBackgroundProps;\n}\n\n/**\n * Type guard for React elements with props.\n */\nexport function isReactElement(node: unknown): node is ReactElement {\n return typeof node === \"object\" && node !== null && \"props\" in node && typeof (node as ReactElement).props === \"object\";\n}\n\n/**\n * Extracts plain text content from React children.\n * Used to get copyable code from pre-highlighted MDX content.\n */\nexport function extractTextFromChildren(children: ReactNode): string {\n if (typeof children === \"string\") return children;\n if (typeof children === \"number\") return String(children);\n if (!children) return \"\";\n\n if (Array.isArray(children)) {\n return children.map(extractTextFromChildren).join(\"\");\n }\n\n if (isReactElement(children)) {\n const props = children.props as { children?: ReactNode };\n if (props.children) {\n return extractTextFromChildren(props.children);\n }\n }\n\n return \"\";\n}\n\n/**\n * Extracts language identifier from pre-highlighted MDX children.\n * Checks `data-language` attribute and `className=\"language-xxx\"` pattern.\n */\nexport function extractLanguageFromChildren(children: ReactNode): string {\n const findLanguage = (node: ReactNode): string | null => {\n if (!node) return null;\n\n if (isReactElement(node)) {\n const props = node.props as {\n \"data-language\"?: string;\n className?: string;\n class?: string;\n children?: ReactNode;\n };\n\n // Check data-language attribute (rehype-pretty-code)\n if (props[\"data-language\"]) {\n return props[\"data-language\"];\n }\n\n // Check className for language-xxx pattern\n const className = props.className || props.class || \"\";\n if (typeof className === \"string\") {\n const match = className.match(/language-([\\w-]+)/i);\n if (match) return match[1];\n }\n\n // Recursively check children\n if (props.children) {\n if (Array.isArray(props.children)) {\n for (const child of props.children) {\n const lang = findLanguage(child);\n if (lang) return lang;\n }\n } else {\n return findLanguage(props.children);\n }\n }\n }\n\n return null;\n };\n\n return findLanguage(children) || \"text\";\n}\n"],
|
|
5
|
+
"mappings": "AAuJO,SAASA,EAAeC,EAAqC,CAClE,OAAO,OAAOA,GAAS,UAAYA,IAAS,MAAQ,UAAWA,GAAQ,OAAQA,EAAsB,OAAU,QACjH,CAMO,SAASC,EAAwBC,EAA6B,CACnE,GAAI,OAAOA,GAAa,SAAU,OAAOA,EACzC,GAAI,OAAOA,GAAa,SAAU,OAAO,OAAOA,CAAQ,EACxD,GAAI,CAACA,EAAU,MAAO,GAEtB,GAAI,MAAM,QAAQA,CAAQ,EACxB,OAAOA,EAAS,IAAID,CAAuB,EAAE,KAAK,EAAE,EAGtD,GAAIF,EAAeG,CAAQ,EAAG,CAC5B,MAAMC,EAAQD,EAAS,MACvB,GAAIC,EAAM,SACR,OAAOF,EAAwBE,EAAM,QAAQ,CAEjD,CAEA,MAAO,EACT,CAMO,SAASC,EAA4BF,EAA6B,CACvE,MAAMG,EAAgBL,GAAmC,CACvD,GAAI,CAACA,EAAM,OAAO,KAElB,GAAID,EAAeC,CAAI,EAAG,CACxB,MAAMG,EAAQH,EAAK,MAQnB,GAAIG,EAAM,eAAe,EACvB,OAAOA,EAAM,eAAe,EAI9B,MAAMG,EAAYH,EAAM,WAAaA,EAAM,OAAS,GACpD,GAAI,OAAOG,GAAc,SAAU,CACjC,MAAMC,EAAQD,EAAU,MAAM,oBAAoB,EAClD,GAAIC,EAAO,OAAOA,EAAM,CAAC,CAC3B,CAGA,GAAIJ,EAAM,SACR,GAAI,MAAM,QAAQA,EAAM,QAAQ,EAC9B,UAAWK,KAASL,EAAM,SAAU,CAClC,MAAMM,EAAOJ,EAAaG,CAAK,EAC/B,GAAIC,EAAM,OAAOA,CACnB,KAEA,QAAOJ,EAAaF,EAAM,QAAQ,CAGxC,CAEA,OAAO,IACT,EAEA,OAAOE,EAAaH,CAAQ,GAAK,MACnC",
|
|
6
|
+
"names": ["isReactElement", "node", "extractTextFromChildren", "children", "props", "extractLanguageFromChildren", "findLanguage", "className", "match", "child", "lang"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface UseCodeCardOptions {
|
|
2
|
+
code: string;
|
|
3
|
+
collapsedHeight: number;
|
|
4
|
+
}
|
|
5
|
+
interface UseCodeCardReturn {
|
|
6
|
+
isExpanded: boolean;
|
|
7
|
+
shouldShowToggle: boolean;
|
|
8
|
+
copied: boolean;
|
|
9
|
+
contentRef: React.RefObject<HTMLDivElement | null>;
|
|
10
|
+
contentMaxHeight: number;
|
|
11
|
+
handleToggle: () => void;
|
|
12
|
+
handleCopy: () => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Shared hook for code card UI behavior.
|
|
16
|
+
* Handles expand/collapse state and copy functionality.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useCodeCard({ code, collapsedHeight }: UseCodeCardOptions): UseCodeCardReturn;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=useCodeCard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCodeCard.d.ts","sourceRoot":"","sources":["../../../../src/components/code/useCodeCard.ts"],"names":[],"mappings":"AAIA,UAAU,kBAAkB;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,UAAU,iBAAiB;IACzB,UAAU,EAAE,OAAO,CAAC;IACpB,gBAAgB,EAAE,OAAO,CAAC;IAC1B,MAAM,EAAE,OAAO,CAAC;IAChB,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IACnD,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,IAAI,CAAC;IACzB,UAAU,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,EAAE,kBAAkB,GAAG,iBAAiB,CA0D5F"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{useState as r,useRef as a,useEffect as u,useCallback as d}from"react";const E=2e3;function b({code:t,collapsedHeight:o}){const[c,f]=r(!1),[s,p]=r(o),[C,i]=r(!1),n=a(null),e=a(null),g=s>o,m=c?s:o;u(()=>{n.current&&p(n.current.scrollHeight)}),u(()=>()=>{e.current&&clearTimeout(e.current)},[]);const T=d(()=>{f(l=>!l)},[]),h=d(async()=>{if(t.trim())try{await navigator.clipboard.writeText(t),i(!0),e.current&&clearTimeout(e.current),e.current=setTimeout(()=>i(!1),E)}catch{}},[t]);return{isExpanded:c,shouldShowToggle:g,copied:C,contentRef:n,contentMaxHeight:m,handleToggle:T,handleCopy:h}}export{b as useCodeCard};
|
|
2
|
+
//# sourceMappingURL=useCodeCard.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/components/code/useCodeCard.ts"],
|
|
4
|
+
"sourcesContent": ["import { useState, useRef, useEffect, useCallback } from \"react\";\n\nconst COPY_FEEDBACK_DURATION_MS = 2000;\n\ninterface UseCodeCardOptions {\n code: string;\n collapsedHeight: number;\n}\n\ninterface UseCodeCardReturn {\n isExpanded: boolean;\n shouldShowToggle: boolean;\n copied: boolean;\n contentRef: React.RefObject<HTMLDivElement | null>;\n contentMaxHeight: number;\n handleToggle: () => void;\n handleCopy: () => Promise<void>;\n}\n\n/**\n * Shared hook for code card UI behavior.\n * Handles expand/collapse state and copy functionality.\n */\nexport function useCodeCard({ code, collapsedHeight }: UseCodeCardOptions): UseCodeCardReturn {\n const [isExpanded, setIsExpanded] = useState(false);\n const [contentHeight, setContentHeight] = useState(collapsedHeight);\n const [copied, setCopied] = useState(false);\n const contentRef = useRef<HTMLDivElement | null>(null);\n const resetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n const shouldShowToggle = contentHeight > collapsedHeight;\n const contentMaxHeight = isExpanded ? contentHeight : collapsedHeight;\n\n // Measure content height on mount and when content changes\n useEffect(() => {\n if (contentRef.current) {\n setContentHeight(contentRef.current.scrollHeight);\n }\n });\n\n // Cleanup timeout on unmount\n useEffect(() => {\n return () => {\n if (resetTimeoutRef.current) {\n clearTimeout(resetTimeoutRef.current);\n }\n };\n }, []);\n\n const handleToggle = useCallback(() => {\n setIsExpanded((prev) => !prev);\n }, []);\n\n const handleCopy = useCallback(async () => {\n if (!code.trim()) return;\n\n try {\n await navigator.clipboard.writeText(code);\n setCopied(true);\n\n if (resetTimeoutRef.current) {\n clearTimeout(resetTimeoutRef.current);\n }\n resetTimeoutRef.current = setTimeout(() => setCopied(false), COPY_FEEDBACK_DURATION_MS);\n } catch (error) {\n // Log error in development, fail gracefully in production\n if (process.env.NODE_ENV === \"development\") {\n console.error(\"[CodeBlock] Failed to copy to clipboard:\", error);\n }\n }\n }, [code]);\n\n return {\n isExpanded,\n shouldShowToggle,\n copied,\n contentRef,\n contentMaxHeight,\n handleToggle,\n handleCopy,\n };\n}\n"],
|
|
5
|
+
"mappings": "AAAA,OAAS,YAAAA,EAAU,UAAAC,EAAQ,aAAAC,EAAW,eAAAC,MAAmB,QAEzD,MAAMC,EAA4B,IAqB3B,SAASC,EAAY,CAAE,KAAAC,EAAM,gBAAAC,CAAgB,EAA0C,CAC5F,KAAM,CAACC,EAAYC,CAAa,EAAIT,EAAS,EAAK,EAC5C,CAACU,EAAeC,CAAgB,EAAIX,EAASO,CAAe,EAC5D,CAACK,EAAQC,CAAS,EAAIb,EAAS,EAAK,EACpCc,EAAab,EAA8B,IAAI,EAC/Cc,EAAkBd,EAA6C,IAAI,EAEnEe,EAAmBN,EAAgBH,EACnCU,EAAmBT,EAAaE,EAAgBH,EAGtDL,EAAU,IAAM,CACVY,EAAW,SACbH,EAAiBG,EAAW,QAAQ,YAAY,CAEpD,CAAC,EAGDZ,EAAU,IACD,IAAM,CACPa,EAAgB,SAClB,aAAaA,EAAgB,OAAO,CAExC,EACC,CAAC,CAAC,EAEL,MAAMG,EAAef,EAAY,IAAM,CACrCM,EAAeU,GAAS,CAACA,CAAI,CAC/B,EAAG,CAAC,CAAC,EAECC,EAAajB,EAAY,SAAY,CACzC,GAAKG,EAAK,KAAK,EAEf,GAAI,CACF,MAAM,UAAU,UAAU,UAAUA,CAAI,EACxCO,EAAU,EAAI,EAEVE,EAAgB,SAClB,aAAaA,EAAgB,OAAO,EAEtCA,EAAgB,QAAU,WAAW,IAAMF,EAAU,EAAK,EAAGT,CAAyB,CACxF,MAAgB,CAKhB,CACF,EAAG,CAACE,CAAI,CAAC,EAET,MAAO,CACL,WAAAE,EACA,iBAAAQ,EACA,OAAAJ,EACA,WAAAE,EACA,iBAAAG,EACA,aAAAC,EACA,WAAAE,CACF,CACF",
|
|
6
|
+
"names": ["useState", "useRef", "useEffect", "useCallback", "COPY_FEEDBACK_DURATION_MS", "useCodeCard", "code", "collapsedHeight", "isExpanded", "setIsExpanded", "contentHeight", "setContentHeight", "copied", "setCopied", "contentRef", "resetTimeoutRef", "shouldShowToggle", "contentMaxHeight", "handleToggle", "prev", "handleCopy"]
|
|
7
|
+
}
|