Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to provide rtl support for react-icons #560

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
31c7a3f
Automate bumping react-icons beta version number
tomi-msft Nov 23, 2021
47fae8d
revert
tomi-msft Nov 23, 2021
8f81a86
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Nov 23, 2021
179a3e7
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Nov 30, 2021
55e3032
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Dec 6, 2021
1cfdd33
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Feb 8, 2022
66d5a05
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Mar 1, 2022
c84e246
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Mar 10, 2022
80bef55
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Apr 14, 2022
37a69cb
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Apr 14, 2022
39b3d32
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Apr 19, 2022
9c0439c
remove manifest.json
tomi-msft Apr 19, 2022
fd04253
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft May 5, 2022
53e77d3
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft May 9, 2022
afb2431
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft May 9, 2022
7507fae
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft May 13, 2022
3b6e5f9
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft May 20, 2022
b502589
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Jun 1, 2022
0d89f78
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Jun 9, 2022
66f1019
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Jun 21, 2022
6832725
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Jun 21, 2022
5327af7
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Jun 30, 2022
7b0bbb9
Release 1.1.179
fluentCI Aug 19, 2022
adab308
Merge branch 'master' of https://github.com/microsoft/fluentui-system…
tomi-msft Sep 22, 2022
10cf5fe
merge with main
tomi-msft Oct 24, 2022
3ec81c8
revert
tomi-msft Oct 24, 2022
74ea042
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Oct 31, 2022
934daf2
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Oct 31, 2022
71ecb5b
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Nov 14, 2022
772f374
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Jan 12, 2023
995334e
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Mar 7, 2023
b0573ab
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Mar 15, 2023
a1e8486
Add context to provide rtl support for react-icons
tomi-msft Mar 17, 2023
8fabcae
remove wrapIcon and consolidate logic
tomi-msft Mar 17, 2023
9da091e
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Mar 23, 2023
9e1d6e9
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Mar 30, 2023
07e1691
Add autoflipping logic to build script
tomi-msft Apr 3, 2023
3a10737
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Apr 3, 2023
dfdc77e
Merge branch 'main' of https://github.com/microsoft/fluentui-system-i…
tomi-msft Apr 18, 2023
12b991c
merge with main
tomi-msft Apr 20, 2023
f7f417f
incorporate changes to icon build into auto-flipping
tomi-msft Apr 21, 2023
1573195
merge
tomi-msft Apr 21, 2023
bc6674b
remove test filter script
tomi-msft Apr 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/react-icons/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ function processFiles(src, dest) {

const indexPath = path.join(dest, 'index.tsx')
// Finally add the interface definition and then write out the index.
indexContents.push('export { FluentIconsProps } from \'./utils/FluentIconsProps.types\'');
indexContents.push('export type { FluentIconsProps } from \'./utils/FluentIconsProps.types\'');
indexContents.push('export { default as wrapIcon } from \'./utils/wrapIcon\'');
indexContents.push('export { default as bundleIcon } from \'./utils/bundleIcon\'');
indexContents.push('export * from \'./utils/useIconState\'');
indexContents.push('export * from \'./utils/constants\'');
indexContents.push('export { IconContextProvider, useIconContext } from \'./contexts/index\'');
indexContents.push('export type { IconContextValue } from \'./contexts/index\'');

fs.writeFileSync(indexPath, indexContents.join('\n'), (err) => {
if (err) throw err;
Expand Down Expand Up @@ -129,17 +131,24 @@ function processFolder(srcPath, destPath, resizable) {
var destFilename = _.camelCase(iconName) // We want them to be camelCase, so access_time would become accessTime here
destFilename = destFilename.replace(destFilename.substring(0, 1), destFilename.substring(0, 1).toUpperCase()) // capitalize the first letter

/**
* This is a placeholder to add the check to see if the icon should be autoflipped
*
* var shouldAutoFlip = shouldFlip(rtlFile);
*/

var iconContent = fs.readFileSync(srcFile, { encoding: "utf8" })

var jsxCode = resizable ? svgr.default.sync(iconContent, svgrOpts, { filePath: file }) : svgr.default.sync(iconContent, svgrOptsSizedIcons, { filePath: file })
var jsCode =
`

const ${destFilename}Icon = (props: FluentIconsProps) => {
export const ${destFilename} = (props: FluentIconsProps) => {
const { fill: primaryFill = 'currentColor', className } = props;
const state = useIconState(props);
${shouldAutoFlip ? 'useAutoFlippingIconStyles(state);' : ''}
return ${jsxCode};
}
export const ${destFilename} = /*#__PURE__*/wrapIcon(/*#__PURE__*/${destFilename}Icon, '${destFilename}');
${destFilename}.displayName = '${destFilename}';
`
iconExports.push(jsCode);
}
Expand All @@ -153,7 +162,6 @@ export const ${destFilename} = /*#__PURE__*/wrapIcon(/*#__PURE__*/${destFilename
}

for(const chunk of iconChunks) {
chunk.unshift(`import wrapIcon from "../utils/wrapIcon";`)
chunk.unshift(`import { FluentIconsProps } from "../utils/FluentIconsProps.types";`)
chunk.unshift(`import * as React from "react";`)
}
Expand Down
13 changes: 13 additions & 0 deletions packages/react-icons/src/contexts/IconDirectionContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react';

const IconDirectionContext = React.createContext<IconDirectionContextValue | undefined>(undefined);

export interface IconDirectionContextValue {
textDirection?: 'ltr' | 'rtl'
}

const IconDirectionContextDefaultValue: IconDirectionContextValue = {};

export const IconDirectionContextProvider = IconDirectionContext.Provider;

export const useIconContext = () => React.useContext(IconDirectionContext) ?? IconDirectionContextDefaultValue;
1 change: 1 addition & 0 deletions packages/react-icons/src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './IconDirectionContext';
15 changes: 15 additions & 0 deletions packages/react-icons/src/utils/useAutoFlippingIconStyles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FluentIconsProps } from "./FluentIconsProps.types";
import { makeStyles, mergeClasses } from "@griffel/react";
import { useIconContext } from "../contexts";

const useFlipStyles = makeStyles({
flipped: {
transform: 'scaleX(-1)'
}
});

export const useAutoFlippingIconStyles = (state: FluentIconsProps) => {
const iconContext = useIconContext();
const styles = useFlipStyles();
state.className = mergeClasses(iconContext.textDirection === 'rtl' && styles.flipped, state.className)
tomi-msft marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions packages/react-icons/src/utils/useIconState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const useRootStyles = makeStyles({

export const useIconState = <TBaseAttributes extends (React.SVGAttributes<SVGElement> | React.HTMLAttributes<HTMLElement>) = React.SVGAttributes<SVGElement>>(props: FluentIconsProps<TBaseAttributes>): Omit<FluentIconsProps<TBaseAttributes>, 'primaryFill'> => {
const { title, primaryFill = "currentColor", ...rest } = props;

tomi-msft marked this conversation as resolved.
Show resolved Hide resolved
const state = {
...rest,
title: undefined,
Expand Down
14 changes: 0 additions & 14 deletions packages/react-icons/src/utils/wrapIcon.tsx

This file was deleted.