1- import { forwardRef , Suspense , lazy } from 'react'
1+ import { forwardRef , lazy , Suspense } from 'react'
22import { styled } from 'styled-components'
33import { useArrayProp } from '../../hooks'
44import { responsiveCodeFontStyle , ResponsiveFontStyleProps } from '../../styles/internal'
55import { codeBaseStyle } from './styles'
66
77const LazyRefractor = lazy ( ( ) => import ( './refractor' ) )
88
9+ /**
10+ * @public
11+ */
12+ export type StringifiableNode =
13+ | string
14+ | number
15+ | bigint
16+ | boolean
17+ | null
18+ | undefined
19+ | Iterable < StringifiableNode >
920/**
1021 * @public
1122 */
1223export interface CodeProps {
1324 as ?: React . ElementType | keyof React . JSX . IntrinsicElements
1425 /** Define the language to use for syntax highlighting. */
1526 language ?: string
27+ children : StringifiableNode
1628 size ?: number | number [ ]
1729 weight ?: string
1830}
@@ -23,17 +35,29 @@ const StyledCode = styled.pre<ResponsiveFontStyleProps>(codeBaseStyle, responsiv
2335 * @public
2436 */
2537export const Code = forwardRef ( function Code (
26- props : CodeProps & Omit < React . HTMLProps < HTMLElement > , 'as' | 'size' > ,
38+ props : CodeProps & Omit < React . HTMLProps < HTMLElement > , 'as' | 'size' | 'children' > ,
2739 ref : React . ForwardedRef < HTMLElement > ,
2840) {
2941 const { children, language, size = 2 , weight, ...restProps } = props
3042
3143 return (
3244 < StyledCode data-ui = "Code" { ...restProps } $size = { useArrayProp ( size ) } $weight = { weight } ref = { ref } >
3345 < Suspense fallback = { < code > { children } </ code > } >
34- < LazyRefractor language = { language } value = { children } />
46+ < LazyRefractor language = { language } value = { stringifyChildren ( children ) } />
3547 </ Suspense >
3648 </ StyledCode >
3749 )
3850} )
3951Code . displayName = 'ForwardRef(Code)'
52+
53+ function stringifyChildren ( children : StringifiableNode ) : string {
54+ if ( ! children || typeof children === 'boolean' ) {
55+ return ''
56+ }
57+
58+ if ( Array . isArray ( children ) ) {
59+ return children . map ( ( c ) => stringifyChildren ( c ) ) . join ( '' )
60+ }
61+
62+ return String ( children )
63+ }
0 commit comments