File tree Expand file tree Collapse file tree 1 file changed +15
-9
lines changed Expand file tree Collapse file tree 1 file changed +15
-9
lines changed Original file line number Diff line number Diff line change 11import React , { useContext } from 'react' ;
22import { Context } from './store' ;
33import { CODE_PREVIEW_PREFIX } from './CodePreview' ;
4- import type { ElementType , ComponentPropsWithoutRef } from 'react' ;
4+
5+ export type TagType = React . ComponentType | keyof JSX . IntrinsicElements ;
6+
7+ export interface CodeProps < Tag extends TagType > extends React . HTMLProps < Tag > {
8+ tagName ?: Tag ;
9+ }
510
611export const Code = React . forwardRef (
7- < T extends ElementType < any > = 'div' > ( props : { as ?: T } & ComponentPropsWithoutRef < T > , ref ? : React . LegacyRef < T > ) => {
8- const { as , className, children, ...htmlProps } = props ;
12+ < Tag extends TagType = 'div' > ( props : CodeProps < Tag > , ref : React . Ref < React . HTMLProps < Tag > > ) => {
13+ const { tagName = 'div' , className, children, ...htmlProps } = props ;
914 const cls = [ `${ CODE_PREVIEW_PREFIX } -code` , className ] . filter ( Boolean ) . join ( ' ' ) . trim ( ) ;
1015 const store = useContext ( Context ) ;
1116 if ( store . collapse ) {
1217 return null ;
1318 }
14- const Comp = as || 'div' ;
15- return (
16- < Comp { ...htmlProps } className = { cls } ref = { ( node ) => { } } >
17- { children }
18- </ Comp >
19- ) ;
19+ const TagName = props . href && typeof tagName === 'string' ? 'a' : tagName ;
20+ const childProps = {
21+ ...htmlProps ,
22+ className : cls ,
23+ ref,
24+ } ;
25+ return React . createElement ( TagName , childProps , children ) ;
2026 } ,
2127) ;
2228
You can’t perform that action at this time.
0 commit comments