Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(codeblock): fall back to spans on Safari (#1487)

Cindy Zhang2cb43c0883de
diff --git a/packages/core/src/CodeBlock/XDSCodeBlock.tsx b/packages/core/src/CodeBlock/XDSCodeBlock.tsx
index ea3eaf4d41..c09c84f662 100644
--- a/packages/core/src/CodeBlock/XDSCodeBlock.tsx
+++ b/packages/core/src/CodeBlock/XDSCodeBlock.tsx
@@ -280,7 +280,9 @@ function renderLines(
chunks.push(
<div
key={start}
- {...mergeProps(stylex.props(styles.lineChunk), {style: {containIntrinsicBlockSize: `auto ${estimatedHeight}`}})}>
+ {...mergeProps(stylex.props(styles.lineChunk), {
+ style: {containIntrinsicBlockSize: `auto ${estimatedHeight}`},
+ })}>
<CodeChunk
lines={chunkLines}
startIndex={start}
@@ -331,6 +333,17 @@ function hasHighlightAPI(): boolean {
);
}
+/**
+ * Safari supports the Highlight API JS objects but has rendering issues
+ * with ::highlight() in code blocks. Detect Safari (WebKit without Chrome)
+ * so we can fall back to spans.
+ */
+function isSafari(): boolean {
+ if (typeof navigator === 'undefined') return false;
+ const ua = navigator.userAgent;
+ return /AppleWebKit/.test(ua) && !/Chrome/.test(ua);
+}
+
/**
* Hook: per-line tokens with sync/async + custom tokenizer compat.
*/
@@ -524,7 +537,8 @@ export function XDSCodeBlock({
const useSpans =
highlightMode === 'spans' ||
- (highlightMode === 'auto' && !hasHighlightAPI());
+ (highlightMode === 'auto' && !hasHighlightAPI()) ||
+ (highlightMode === 'auto' && isSafari());
const lines = useMemo(() => {
const l = code.split('\n');
@@ -645,7 +659,9 @@ export function XDSCodeBlock({
const codeBody = (
<div
ref={scrollContainerRef}
- {...mergeProps(stylex.props(styles.scrollContainer), {style: scrollStyle})}>
+ {...mergeProps(stylex.props(styles.scrollContainer), {
+ style: scrollStyle,
+ })}>
<div
{...stylex.props(
styles.codeWrapper,