|
| 1 | +import React, { useMemo } from 'react' |
| 2 | + |
| 3 | +import { |
| 4 | + counter as faCounter, |
| 5 | + text as faText, |
| 6 | + parse as faParse, |
| 7 | + Transform, |
| 8 | + SizeProp, |
| 9 | +} from '@fortawesome/fontawesome-svg-core' |
| 10 | + |
| 11 | +import { makeReactConverter } from '../converter' |
| 12 | +import { CSSVariables } from '../types/css-variables' |
| 13 | +import { LAYER_CLASSES, STYLE_CLASSES } from '../utils/constants' |
| 14 | +import { withPrefix } from '../utils/get-class-list-from-props' |
| 15 | + |
| 16 | +type Attributes = React.HTMLAttributes<HTMLSpanElement> |
| 17 | + |
| 18 | +interface FontAwesomeLayersProps extends Attributes { |
| 19 | + children: React.ReactNode |
| 20 | + className?: string | undefined |
| 21 | + size?: SizeProp | undefined |
| 22 | +} |
| 23 | + |
| 24 | +const DEFAULT_CLASSNAMES = `${LAYER_CLASSES.default} ${STYLE_CLASSES.fixedWidth}` |
| 25 | + |
| 26 | +/** |
| 27 | + * React Component that allows you to stack multiple Font Awesome icons on top of each other, |
| 28 | + * or to layer with text or a counter. |
| 29 | + * |
| 30 | + * @see https://docs.fontawesome.com/web/style/layer |
| 31 | + * |
| 32 | + * @example |
| 33 | + * ```tsx |
| 34 | + * import { FontAwesomeIcon, FontAwesomeLayers } from '@fortawesome/react-fontawesome' |
| 35 | + * import { faBookmark, faCircle, faCheck, faHeart, faMoon, faPlay, faStar, faSun } from '@fortawesome/free-solid-svg-icons' |
| 36 | + * |
| 37 | + * // React versions of the examples from the FontAwesome Web Docs |
| 38 | + * export const Examples = () => ( |
| 39 | + * <div className="fa-4x"> |
| 40 | + * <FontAwesomeLayers> |
| 41 | + * <FontAwesomeIcon icon={faCircle} color="tomato" /> |
| 42 | + * <FontAwesomeIcon icon={faCheck} inverse transform="shrink-6" /> |
| 43 | + * </FontAwesomeLayers> |
| 44 | + * <FontAwesomeLayers> |
| 45 | + * <FontAwesomeIcon icon={faBookmark} /> |
| 46 | + * <FontAwesomeIcon icon={faHeart} color="tomato" transform="shrink-10 up-2" /> |
| 47 | + * </FontAwesomeLayers> |
| 48 | + * <FontAwesomeLayers> |
| 49 | + * <FontAwesomeIcon icon={faPlay} transform="rotate--90 grow-4" /> |
| 50 | + * <FontAwesomeIcon icon={faSun} inverse transform="shrink-10 up-2" /> |
| 51 | + * <FontAwesomeIcon icon={faMoon} inverse transform="shrink-11 down-4.2 left-4" /> |
| 52 | + * <FontAwesomeIcon icon={faStar} inverse transform="shrink-11 down-4.2 right-4" /> |
| 53 | + * </FontAwesomeLayers> |
| 54 | + * </div> |
| 55 | + * ) |
| 56 | + * ``` |
| 57 | + * |
| 58 | + * For examples using Text or Counter components: |
| 59 | + * @see {@link LayersText} |
| 60 | + * @see {@link LayersCounter} |
| 61 | + */ |
| 62 | +const FontAwesomeLayers = ({ |
| 63 | + children, |
| 64 | + className, |
| 65 | + size, |
| 66 | + ...attributes |
| 67 | +}: FontAwesomeLayersProps) => { |
| 68 | + const prefixedDefaultClasses = withPrefix(DEFAULT_CLASSNAMES) |
| 69 | + const classes = className |
| 70 | + ? `${prefixedDefaultClasses} ${className}` |
| 71 | + : prefixedDefaultClasses |
| 72 | + |
| 73 | + const element = ( |
| 74 | + <span {...attributes} className={classes}> |
| 75 | + {children} |
| 76 | + </span> |
| 77 | + ) |
| 78 | + |
| 79 | + if (size) { |
| 80 | + return <div className={withPrefix(`fa-${size}`)}>{element}</div> |
| 81 | + } |
| 82 | + |
| 83 | + return element |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Text component to be used within a `FontAwesomeLayers` component. |
| 88 | + * |
| 89 | + * @see https://docs.fontawesome.com/web/style/layer |
| 90 | + * |
| 91 | + * @example |
| 92 | + * ```tsx |
| 93 | + * import { FontAwesomeLayers, LayersText } from '@fortawesome/react-fontawesome' |
| 94 | + * import { faCalendar, faCertificate } from '@fortawesome/free-solid-svg-icons' |
| 95 | + * |
| 96 | + * // React versions of the examples from the FontAwesome Web Docs |
| 97 | + * export const Examples = () => ( |
| 98 | + * <div className="fa-4x"> |
| 99 | + * <FontAwesomeLayers> |
| 100 | + * <FontAwesomeIcon icon={faCalendar} /> |
| 101 | + * <LayersText |
| 102 | + * text="27" |
| 103 | + * inverse |
| 104 | + * style={{ fontWeight: '900' }} |
| 105 | + * transform="shrink-8 down-3" |
| 106 | + * /> |
| 107 | + * </FontAwesomeLayers> |
| 108 | + * <FontAwesomeLayers> |
| 109 | + * <FontAwesomeIcon icon={faCertificate} /> |
| 110 | + * <LayersText |
| 111 | + * text="NEW" |
| 112 | + * inverse |
| 113 | + * style={{ fontWeight: '900' }} |
| 114 | + * transform="shrink-11.5 rotate--30" |
| 115 | + * /> |
| 116 | + * </FontAwesomeLayers> |
| 117 | + * </div> |
| 118 | + * ) |
| 119 | + * ``` |
| 120 | + */ |
| 121 | +const LayersText = ({ |
| 122 | + text, |
| 123 | + className, |
| 124 | + inverse, |
| 125 | + transform, |
| 126 | + style, |
| 127 | + ...attributes |
| 128 | +}: Attributes & { |
| 129 | + text: string |
| 130 | + className?: string | undefined |
| 131 | + inverse?: boolean | undefined |
| 132 | + transform?: string | Transform | undefined |
| 133 | + style?: (React.CSSProperties & CSSVariables) | undefined |
| 134 | +}) => { |
| 135 | + const textAbstractElement = useMemo(() => { |
| 136 | + const textObject = faText(text, { |
| 137 | + classes: [ |
| 138 | + ...(className?.split(' ') || []), |
| 139 | + ...(inverse ? [STYLE_CLASSES.inverse] : []), |
| 140 | + ], |
| 141 | + transform: |
| 142 | + typeof transform === 'string' |
| 143 | + ? faParse.transform(transform) |
| 144 | + : transform, |
| 145 | + }) |
| 146 | + |
| 147 | + console.log(textObject.abstract[0]) |
| 148 | + |
| 149 | + return textObject.abstract[0] |
| 150 | + }, [text, transform, className, inverse]) |
| 151 | + |
| 152 | + return makeReactConverter(textAbstractElement, { ...attributes, style }) |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Counter component to be used within a `FontAwesomeLayers` component. |
| 157 | + * |
| 158 | + * @see https://docs.fontawesome.com/web/style/layer |
| 159 | + * |
| 160 | + * @example |
| 161 | + * ```tsx |
| 162 | + * import { FontAwesomeLayers, LayersCounter } from '@fortawesome/react-fontawesome' |
| 163 | + * import { faEnvelope } from '@fortawesome/free-solid-svg-icons' |
| 164 | + * |
| 165 | + * // React version of the example from the FontAwesome Web Docs |
| 166 | + * export const Example = ({ count = 1419 }) => ( |
| 167 | + * <FontAwesomeLayers size="4x"> |
| 168 | + * <FontAwesomeIcon icon={faEnvelope} /> |
| 169 | + * <LayersCounter count={count.toLocaleString()} style={{ backgroundColor: 'tomato' }} /> |
| 170 | + * </FontAwesomeLayers> |
| 171 | + * ) |
| 172 | + * ``` |
| 173 | + */ |
| 174 | +const LayersCounter = ({ |
| 175 | + count, |
| 176 | + className, |
| 177 | + style, |
| 178 | + ...attributes |
| 179 | +}: Attributes & { |
| 180 | + count: number | string |
| 181 | + className?: string | undefined |
| 182 | + style?: (React.CSSProperties & CSSVariables) | undefined |
| 183 | +}) => { |
| 184 | + const counterAbstractElement = useMemo( |
| 185 | + () => |
| 186 | + faCounter(count, { |
| 187 | + classes: className?.split(' '), |
| 188 | + }).abstract[0], |
| 189 | + [count, className], |
| 190 | + ) |
| 191 | + |
| 192 | + return makeReactConverter(counterAbstractElement, { ...attributes, style }) |
| 193 | +} |
| 194 | + |
| 195 | +export { FontAwesomeLayers, LayersText, LayersCounter } |
0 commit comments