import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; /** Rest props are spread to the root div (single/multi, except `aria-label`, `aria-labelledby`, and `aria-describedby`, which go on the code container), the copy button (inline), or the span (inline with `hideCopyButton`). */ type $RestProps = SvelteHTMLElements["div"] & SvelteHTMLElements["button"] & SvelteHTMLElements["span"]; type $Props = { /** * Set the type of code snippet. * @default "single" */ type?: "single" | "inline" | "multi"; /** * Set the code snippet text. * Alternatively, use the default slot. * * When `code` is unset, the copy button copies the rendered text of the default slot. * @example * ```svelte * {code} * ``` */ code?: string | undefined; /** * By default, this component uses `navigator.clipboard.writeText` API to copy text to the user's clipboard, * with a `document.execCommand("copy")` fallback. Failures reject so the component can show * `errorFeedback` and dispatch `copy:error`. * * Provide a custom function to override this behavior. * @default copyText */ copy?: (code: string) => void | Promise; /** * Set to `true` to expand a multi-line code snippet (type="multi"). * @default false */ expanded?: boolean; /** * Set to `true` to hide the copy button * @default false */ hideCopyButton?: boolean; /** * Set to `true` for the disabled variant. * @default false */ disabled?: boolean; /** * Set to `true` to wrap the text. * * NOTE: this prop only works with the `type="multi"` variant. * @default false */ wrapText?: boolean; /** * Set to `true` for a single-line or multi-line snippet to fill its * container instead of stopping at 48rem. * @default false */ fullWidth?: boolean; /** * Set to `true` to display the skeleton state * @default false */ skeleton?: boolean; /** * Specify the ARIA label for the copy button icon. */ copyButtonDescription?: string | undefined; /** * Specify the ARIA label of the copy button. * @default "Copy code" */ copyLabel?: string; /** * Specify the ARIA label of the code snippet container (single/multi variants). * @default "Code snippet" */ codeLabel?: string; /** * Specify the feedback text displayed when clicking the snippet * @default "Copied!" */ feedback?: string; /** * Specify the feedback text displayed when copying fails * @default "Failed to copy" */ errorFeedback?: string; /** * Set the timeout duration (ms) to display feedback text * @default 2000 */ feedbackTimeout?: number; /** * Specify an icon to render on the copy button during the feedback window * (e.g. after copying). When unset, the copy icon is always shown. * * NOTE: this prop does not apply to the `type="inline"` variant. */ feedbackIcon?: Icon | undefined; /** * Specify the show less text. * * NOTE: this prop only works with the `type="multi"` variant. * @default "Show less" */ showLessText?: string; /** * Specify the show more text. * * NOTE: this prop only works with the `type="multi"` variant. * @default "Show more" */ showMoreText?: string; /** * Set to `false` to hide the show more/less button. * * When hidden, overflowing multi-line content scrolls inside the * collapsed container instead of being clipped with no affordance. * * NOTE: this prop only works with the `type="multi"` variant. * @default true */ showMoreLess?: boolean; /** * Specify the maximum number of rows shown when collapsed. * Set to `0` for no maximum. * * NOTE: this prop only works with the `type="multi"` variant. * A row is 1rem, the code's line height (16px by default). * @default 15 */ maxCollapsedNumberOfRows?: number; /** * Specify the maximum number of rows shown when expanded. * Set to `0` for no maximum (default). * * NOTE: this prop only works with the `type="multi"` variant. * A row is 1rem, the code's line height (16px by default). * @default 0 */ maxExpandedNumberOfRows?: number; /** * Specify the minimum number of rows shown when collapsed. * Set to `0` for no minimum. * * NOTE: this prop only works with the `type="multi"` variant. * A row is 1rem, the code's line height (16px by default). * @default 3 */ minCollapsedNumberOfRows?: number; /** * Specify the minimum number of rows shown when expanded. * Set to `0` for no minimum. * * NOTE: this prop only works with the `type="multi"` variant. * A row is 1rem, the code's line height (16px by default). * @default 16 */ minExpandedNumberOfRows?: number; /** * Set an id for the code element * @default uniqueId() */ id?: string; /** * Obtain a reference to the pre HTML element. * @default null */ ref?: null | HTMLPreElement; /** * Obtain a reference to the underlying copy button element. * @default null */ copyRef?: null | HTMLButtonElement; /** * Set how the "Copied!" feedback tooltip is rendered. * By default, it is rendered in a portal so it is never clipped by an * `overflow: hidden` container. Set to `false` to use Carbon's inline * feedback caret instead. */ portalTooltip?: boolean | undefined; /** * Set the tooltip placement in floating-ui form: the side, then `start` * or `end` along it, as Popover's `align` takes. Above and below, * `start` and `end` name inline sides, so they swap under `dir="rtl"`. * When set, takes precedence over `tooltipPosition` and `tooltipAlignment`. */ tooltipAlign?: "top" | "top-start" | "top-end" | "bottom" | "bottom-start" | "bottom-end" | "left" | "left-start" | "left-end" | "right" | "right-start" | "right-end" | undefined; /** * Set the position of the feedback tooltip relative to the copy button. * @deprecated use `tooltipAlign` * @default "bottom" */ tooltipPosition?: "top" | "right" | "bottom" | "left"; /** * Set the alignment of the feedback tooltip relative to the copy button. * @deprecated use `tooltipAlign` * @default "center" */ tooltipAlignment?: "start" | "center" | "end"; children?: (this: void) => void; [key: `data-${string}`]: unknown; }; export type CodeSnippetProps = Omit<$RestProps, keyof $Props> & $Props; export default class CodeSnippet extends SvelteComponentTyped< CodeSnippetProps, { animationend: WindowEventMap["animationend"]; click: WindowEventMap["click"]; collapse: CustomEvent; copy: CustomEvent; "copy:error": CustomEvent<{ error: unknown }>; expand: CustomEvent; mouseenter: WindowEventMap["mouseenter"]; "mouseenter:copy-button": CustomEvent; mouseleave: WindowEventMap["mouseleave"]; "mouseleave:copy-button": CustomEvent; mouseover: WindowEventMap["mouseover"]; }, { default: Record } > {}