Skip to content

Commit ad42835

Browse files
committed
feat: custom cssPrefix support
1 parent c942c6a commit ad42835

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

.tool-versions

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
nodejs 22.17.1
2-
python 3.9.19 2.7.18

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"require": "./dist/index.cjs",
1515
"default": "./dist/index.js"
1616
},
17+
"./components/rsc/CustomPrefixProvider": {
18+
"types": "./dist/components/rsc/CustomPrefixProvider.d.ts",
19+
"import": "./dist/components/rsc/CustomPrefixProvider.js",
20+
"require": "./dist/components/rsc/CustomPrefixProvider.cjs",
21+
"default": "./dist/components/rsc/CustomPrefixProvider.js"
22+
},
1723
"./package.json": "./package.json"
1824
},
1925
"homepage": "https://github.com/FortAwesome/react-fontawesome",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use client'
2+
3+
import { config } from '@fortawesome/fontawesome-svg-core'
4+
5+
interface CustomPrefixProviderProps {
6+
customPrefix: string
7+
}
8+
9+
export const CustomPrefixProvider = ({
10+
customPrefix,
11+
}: CustomPrefixProviderProps): null => {
12+
config.cssPrefix = customPrefix
13+
14+
return null
15+
}

src/utils/__tests__/get-class-list-from-props.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
config,
23
PullProp,
34
RotateProp,
45
SizeProp,
@@ -150,4 +151,20 @@ describe('get class list', () => {
150151
])
151152
},
152153
)
154+
155+
describe('with custom cssPrefix configured', () => {
156+
const customPrefix = 'custom-prefix'
157+
158+
beforeEach(() => {
159+
config.cssPrefix = customPrefix
160+
})
161+
162+
it('should use custom cssPrefix for all classes', () => {
163+
const classList = getClassListFromProps(props)
164+
const classes = expectedClasses.map((cls) =>
165+
cls.replace('fa-', `${customPrefix}-`),
166+
)
167+
expect(classList).toStrictEqual(classes)
168+
})
169+
})
153170
})

src/utils/get-class-list-from-props.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
import { config } from '@fortawesome/fontawesome-svg-core'
2+
13
import {
24
ANIMATION_CLASSES,
3-
IS_VERSION_7_OR_LATER,
45
PULL_CLASSES,
56
ROTATE_CLASSES,
67
SIZE_CLASSES,
78
STYLE_CLASSES,
9+
IS_VERSION_7_OR_LATER,
810
} from './constants'
911
import { FontAwesomeIconProps } from '../types/icon-props'
1012

13+
function withCustomPrefix(cls: string): string {
14+
const customPrefix = config.cssPrefix || config.familyPrefix || 'fa'
15+
return customPrefix === 'fa' ? cls : cls.replace('fa-', `${customPrefix}-`)
16+
}
17+
1118
/**
1219
* Get CSS class list from a props object.
1320
* This function maps our React props to the corresponding CSS class names from Font Awesome.
@@ -80,5 +87,11 @@ export function getClassListFromProps(props: FontAwesomeIconProps): string[] {
8087
if (rotateBy) result.push(STYLE_CLASSES.rotateBy)
8188
if (widthAuto) result.push(STYLE_CLASSES.widthAuto)
8289

83-
return result
90+
const prefix = config.cssPrefix || config.familyPrefix || 'fa'
91+
92+
return prefix === 'fa'
93+
? result
94+
: // TODO: see if we can achieve custom prefix support without iterating
95+
// eslint-disable-next-line unicorn/no-array-callback-reference
96+
result.map(withCustomPrefix)
8497
}

tsup.config.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
import { defineConfig } from 'tsup'
1+
import { defineConfig, type Options } from 'tsup'
22

3-
export default defineConfig({
4-
name: 'react-fontawesome',
5-
entry: ['src/index.ts'],
3+
const defaultConfig: Options = {
64
format: ['cjs', 'esm'],
75
dts: true,
86
bundle: true,
97
clean: true,
108
minify: false,
119
treeshake: true,
12-
outDir: 'dist',
13-
})
10+
}
11+
12+
export default defineConfig([
13+
// Build config for React Fontawesome library
14+
{
15+
...defaultConfig,
16+
name: 'react-fontawesome',
17+
entry: ['src/index.ts'],
18+
outDir: 'dist',
19+
},
20+
// Build config for React Server Components
21+
{
22+
...defaultConfig,
23+
name: 'react-fontawesome-rsc',
24+
entry: ['src/components/rsc/*.tsx'],
25+
outDir: 'dist/components/rsc',
26+
bundle: false,
27+
treeshake: false,
28+
},
29+
])

0 commit comments

Comments
 (0)