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 4 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
20 changes: 15 additions & 5 deletions packages/react-icons/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const _ = require("lodash");

const SRC_PATH = argv.source;
const DEST_PATH = argv.dest;
const RTL_FILTER_PATH =argv.filter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter is a confusing name for this arg. How about flip_in_rtl?

Ditto for the file name: flip_in_rtl.txt instead of rtl.txt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly even autoflip_in_rtl

const TSX_EXTENSION = '.tsx'

if (!SRC_PATH) {
Expand All @@ -18,12 +19,17 @@ if (!SRC_PATH) {
if (!DEST_PATH) {
throw new Error("Output destination folder not specified by --dest");
}
if (!RTL_FILTER_PATH) {
throw new Error("Filter folder not specified by --filter");
}

if (!fs.existsSync(DEST_PATH)) {
fs.mkdirSync(DEST_PATH);
}

processFiles(SRC_PATH, DEST_PATH)
const filterFile = fs.readFileSync(RTL_FILTER_PATH, { encoding: 'utf8' })
var rtlArray = filterFile.split(/\r?\n/);

function processFiles(src, dest) {
/** @type string[] */
Expand Down Expand Up @@ -66,11 +72,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 @@ -122,6 +130,8 @@ function processFolder(srcPath, destPath, resizable) {
if(resizable && !file.includes("20")) {
return
}
// Check to see if the svg should be autoflipped
var shouldAutoFlip = rtlArray.includes(file);
var iconName = file.substr(0, file.length - 4) // strip '.svg'
iconName = iconName.replace("ic_fluent_", "") // strip ic_fluent_
iconName = resizable ? iconName.replace("20", "") : iconName
Expand All @@ -133,12 +143,13 @@ function processFolder(srcPath, destPath, resizable) {
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 @@ -152,7 +163,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
2 changes: 1 addition & 1 deletion packages/react-icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"cleanSvg": "rm -rf ./intermediate",
"copy": "node ../../importer/generate.js --source=../../assets --dest=./intermediate --extension=svg --target=react",
"copy:font-files": "cpy './src/utils/fonts/*.{ttf,woff,woff2,json}' ./lib/utils/fonts/. && cpy './src/utils/fonts/*.{ttf,woff,woff2,json}' ./lib-cjs/utils/fonts/.",
"convert:svg": "node convert.js --source=./intermediate --dest=./src",
"convert:svg": "node convert.js --source=./intermediate --dest=./src --filter=../../importer/rtl.txt",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see rtl.txt included in this PR? If there is an existing file, it might be used for a different purpose? We probably want to create a new file for this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rtl.txt file is here.

I am wary about creating a new file for this because this file was already created by the designers to track icons that should be flipped in rtl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to track down with the Android folks (that's the only platform previously using the rtl.txt file) what is/isn't there. RTL wasn't kept up with, for the most part, as other platforms didn't need/use it.
We should also collaborate with the Apple folks on that, as they are the platform that has the most RTL/LTR support in, and it's supported by the platform itself, but we need to make sure everything has equivalency.

"convert:fonts": "node convert-font.js --source=./src/utils/fonts --dest=./src/fonts --codepointDest=./src/utils/fonts",
"generate:font-regular": "node ../../importer/generateFont.js --source=intermediate --dest=src/utils/fonts --iconType=Regular --codepoints=../../fonts/FluentSystemIcons-Regular.json",
"generate:font-filled": "node ../../importer/generateFont.js --source=intermediate --dest=src/utils/fonts --iconType=Filled --codepoints=../../fonts/FluentSystemIcons-Filled.json",
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.