UNPKG

@wordpress/components

Version:
1 lines • 4.75 kB
{"version":3,"file":"index.cjs","names":["Dashicon","isValidElement","cloneElement","createElement","SVG"],"sources":["../../src/icon/index.tsx"],"sourcesContent":["import type { ComponentType, CSSProperties, HTMLProps, SVGProps } from 'react';\nimport {\n\tcloneElement,\n\tcreateElement,\n\tisValidElement,\n} from '@wordpress/element';\nimport { SVG } from '@wordpress/primitives';\nimport Dashicon from '../dashicon';\nimport type { IconKey as DashiconIconKey } from '../dashicon/types';\n\nexport type IconType =\n\t| DashiconIconKey\n\t| ComponentType< { size?: number } >\n\t| ( ( props: { size?: number } ) => React.JSX.Element )\n\t| React.JSX.Element;\n\n/* The sizing props forwarded to an icon element that is not an `SVG` or a `Dashicon`. */\ntype SizeProps = {\n\tsize?: number;\n\twidth?: number | string;\n\theight?: number | string;\n\tstyle?: CSSProperties;\n};\n\ntype AdditionalProps< T > =\n\tT extends ComponentType< infer U >\n\t\t? U\n\t\t: T extends DashiconIconKey\n\t\t\t? SVGProps< SVGSVGElement >\n\t\t\t: {};\n\nexport type Props = {\n\t/**\n\t * The icon to render. In most cases, you should use an icon from\n\t * [the `@wordpress/icons` package](https://wordpress.github.io/gutenberg/?path=/story/icons-icon--library).\n\t *\n\t * Other supported values are: component instances, functions,\n\t * [Dashicons](https://developer.wordpress.org/resource/dashicons/)\n\t * (specified as strings), and `null`.\n\t *\n\t * The `size` value, as well as any other additional props, will be passed through.\n\t *\n\t * @default null\n\t */\n\ticon?: IconType | null;\n\t/**\n\t * The size (width and height) of the icon.\n\t *\n\t * Defaults to `20` when `icon` is a string (i.e. a Dashicon id), otherwise `24`.\n\t *\n\t * @default `'string' === typeof icon ? 20 : 24`.\n\t */\n\tsize?: number;\n} & AdditionalProps< IconType >;\n\n/**\n * Renders a raw icon without any initial styling or wrappers.\n *\n * ```jsx\n * import { wordpress } from '@wordpress/icons';\n *\n * <Icon icon={ wordpress } />\n * ```\n */\nfunction Icon( {\n\ticon = null,\n\tsize = 'string' === typeof icon ? 20 : 24,\n\t...additionalProps\n}: Props ) {\n\tif ( 'string' === typeof icon ) {\n\t\treturn (\n\t\t\t<Dashicon\n\t\t\t\ticon={ icon }\n\t\t\t\tsize={ size }\n\t\t\t\t{ ...( additionalProps as HTMLProps< HTMLSpanElement > ) }\n\t\t\t/>\n\t\t);\n\t}\n\n\tif ( isValidElement( icon ) && Dashicon === icon.type ) {\n\t\treturn cloneElement( icon, {\n\t\t\t...additionalProps,\n\t\t} );\n\t}\n\n\tif ( 'function' === typeof icon ) {\n\t\treturn createElement( icon, {\n\t\t\tsize,\n\t\t\t...additionalProps,\n\t\t} );\n\t}\n\n\tif ( isValidElement< SizeProps >( icon ) ) {\n\t\tconst { style: consumerStyle, ...restProps } =\n\t\t\tadditionalProps as SVGProps< SVGSVGElement >;\n\t\tconst mergedStyle =\n\t\t\ticon.props.style || consumerStyle\n\t\t\t\t? { ...icon.props.style, ...consumerStyle }\n\t\t\t\t: undefined;\n\t\tconst styleProps = mergedStyle ? { style: mergedStyle } : {};\n\n\t\tif ( icon.type === 'svg' || icon.type === SVG ) {\n\t\t\tconst appliedProps = {\n\t\t\t\t...icon.props,\n\t\t\t\twidth: size,\n\t\t\t\theight: size,\n\t\t\t\t...restProps,\n\t\t\t\t// Merge styles so the icon's intrinsic style (e.g. `fill: none` on\n\t\t\t\t// stroke-based icons) is preserved unless the consumer overrides\n\t\t\t\t// the same property explicitly.\n\t\t\t\t...styleProps,\n\t\t\t};\n\n\t\t\treturn <SVG { ...appliedProps } />;\n\t\t}\n\n\t\treturn cloneElement( icon, {\n\t\t\tsize,\n\t\t\twidth: size,\n\t\t\theight: size,\n\t\t\t...restProps,\n\t\t\t// Merge styles so the icon's intrinsic style is preserved unless\n\t\t\t// the consumer overrides the same property explicitly.\n\t\t\t...styleProps,\n\t\t} );\n\t}\n\n\treturn icon;\n}\n\nexport default Icon;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,SAAS,KAAM,EACd,OAAO,MACP,OAAO,aAAa,OAAO,OAAO,KAAK,IACvC,GAAG,mBACO;CACV,IAAK,aAAa,OAAO,MACxB,OACC,iBAAA,GAAA,kBAAA,IAAA,CAACA,sBAAAA,SAAD;EACQ;EACA;EACP,GAAO;CACP,CAAA;CAIH,KAAA,GAAKC,mBAAAA,eAAAA,CAAgB,IAAK,KAAKD,sBAAAA,YAAa,KAAK,MAChD,QAAA,GAAOE,mBAAAA,aAAAA,CAAc,MAAM,EAC1B,GAAG,gBACJ,CAAE;CAGH,IAAK,eAAe,OAAO,MAC1B,QAAA,GAAOC,mBAAAA,cAAAA,CAAe,MAAM;EAC3B;EACA,GAAG;CACJ,CAAE;CAGH,KAAA,GAAKF,mBAAAA,eAAAA,CAA6B,IAAK,GAAI;EAC1C,MAAM,EAAE,OAAO,eAAe,GAAG,cAChC;EACD,MAAM,cACL,KAAK,MAAM,SAAS,gBACjB;GAAE,GAAG,KAAK,MAAM;GAAO,GAAG;EAAc,IACxC,KAAA;EACJ,MAAM,aAAa,cAAc,EAAE,OAAO,YAAY,IAAI,CAAC;EAE3D,IAAK,KAAK,SAAS,SAAS,KAAK,SAASG,sBAAAA,KAAM;GAC/C,MAAM,eAAe;IACpB,GAAG,KAAK;IACR,OAAO;IACP,QAAQ;IACR,GAAG;IAIH,GAAG;GACJ;GAEA,OAAO,iBAAA,GAAA,kBAAA,IAAA,CAACA,sBAAAA,KAAD,EAAK,GAAK,aAAgB,CAAA;EAClC;EAEA,QAAA,GAAOF,mBAAAA,aAAAA,CAAc,MAAM;GAC1B;GACA,OAAO;GACP,QAAQ;GACR,GAAG;GAGH,GAAG;EACJ,CAAE;CACH;CAEA,OAAO;AACR"}