Skip to content

Use @primer/octicons-react icons - #3227

Merged
fregante merged 16 commits into
refined-github:masterfrom
FloEdelmann:octicons-react
Nov 30, 2020
Merged

Use @primer/octicons-react icons#3227
fregante merged 16 commits into
refined-github:masterfrom
FloEdelmann:octicons-react

Conversation

@FloEdelmann

Copy link
Copy Markdown
Member

I've tried to use the official @primer/octicons-react package, as suggested in #3147 (comment).

I think this is not possible, at least not for now. (Or I am not skilled enough in TypeScript / JSX / React / dom-chef.)

  1. The default props are not applied.

    In particular the size prop is missing, so all the icons are huge.

    Screenshot_2020-06-12_21-14-51

    Relevant code snippets in octicons-react/dist/index.esm.js:

    Screenshot_2020-06-12_21-13-05


    Screenshot_2020-06-12_21-13-31

  2. There is no nice way to pass props to an icon function in dom-chef, as <SomeIcon some-prop="foo"/> is compiled to createElement(SomeIcon(), {someProp: 'foo'}) instead of createElement(SomeIcon({someProp: 'foo'})).

  3. The icons don't have specific classes, i.e. we have to add the classes manually where we need it. E.g. this selector will break:

    https://github.com/sindresorhus/refined-github/blob/4dabb8ce0539222101d39c5329857dafb88327b6/source/features/toggle-files-button.css#L11-L12

Nevertheless, I wanted to share my progress, maybe someone else can figure out how to fix those issues 🤞

@fregante

fregante commented Jun 12, 2020

Copy link
Copy Markdown
Member

3. The icons don't have specific classes, i.e. we have to add the classes manually where we need it. E.g. this selector will break:

Yeah not worth it then. If we make this change, it should make things easier during development, not harder/more verbose.

Those classes are important because sometimes we just add sections and those classes just pick up the existing styles that GitHub put in place. The less CSS we have, the less we have to update when the move things by 3px.

@FloEdelmann

FloEdelmann commented Jun 14, 2020

Copy link
Copy Markdown
Member Author

I created two PRs in upstream repos:

  1. Pass defaultProps to element function vadimdemedes/dom-chef#63 to support defaultProps in dom-chef → resolves problem 1
  2. Add icon-specific className to React default props primer/octicons#453 to add icon-specific classes to the components' defaultProps → resolves problem 3

That would leave us with just problem 2, but we could still override the resulting attributes manually instead of passing custom props, like mentioned in the suggested dom-chef readme update:

<SomeIcon width={32} height={32} /> // <svg … width="32" height="32">
<SomeIcon size={32} /> // <svg  width="16" height="16" size="32">

Now let's see if those PRs get merged 😄

@fregante fregante mentioned this pull request Jul 6, 2020
@fregante

Copy link
Copy Markdown
Member

Thank you for working on this! I hope primer/octicons#453 can be resolved soon

@fregante fregante mentioned this pull request Oct 5, 2020
4 tasks
@fregante
fregante marked this pull request as ready for review November 28, 2020 01:02
@fregante

fregante commented Nov 28, 2020

Copy link
Copy Markdown
Member

Let's get this over with:

Screen Shot 2020-11-27 at 18 50 50

I added a webpack loader to take care of this without having to wait forever for Octicons.

Please check, speak now or forever hold your peace.

I think the last step is to make sure that the icons are tree shaken: primer/octicons#405

@fregante fregante changed the title Experiment with @primer/octicons-react icons Use @primer/octicons-react icons Nov 28, 2020
Comment thread webpack.config.ts
Comment thread webpack.config.ts Outdated

@yakov116 yakov116 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

image
image

yakov116 and others added 2 commits November 28, 2020 18:49
Co-authored-by: Flo Edelmann <florian-edelmann@online.de>
@fregante

fregante commented Nov 29, 2020

Copy link
Copy Markdown
Member

@yakov116 that was due to the broken regex suggested by the linter, probably

@fregante

Copy link
Copy Markdown
Member
❯ nr build

        background.js ⏤  18 kB
  browser-polyfill.js ⏤  3.93 kB
          options.css ⏤  1.78 kB
           options.js ⏤  32.5 kB
   refined-github.css ⏤  11.6 kB (+4 B)
    refined-github.js ⏤  168 kB (+44.2 kB)
 resolve-conflicts.js ⏤  1.03 kB

[webpack-cli] Compilation finished

Given the file increase, the icons are definitely not being tree-shaken 😬

Hopefully the beautifier is enough for AMO reviewers. If not, I have to figure out which Terser compress flag actually tree shakes
@fregante

fregante commented Nov 29, 2020

Copy link
Copy Markdown
Member
❯ nr build

        background.js ⏤  17.2 kB (-806 B)
  browser-polyfill.js ⏤  3.9 kB (-34 B)
          options.css ⏤  1.78 kB
           options.js ⏤  31.7 kB (-799 B)
   refined-github.css ⏤  11.6 kB (+4 B)
    refined-github.js ⏤  110 kB (-12.9 kB)
 resolve-conflicts.js ⏤  1.04 kB (+12 B)

[webpack-cli] Compilation finished

Comment thread source/features/forked-to.tsx Outdated
Comment thread source/globals.d.ts
declare module 'react' {
const FC = (): JSX.Element => JSX.Element;
const React = {FC};
export default React;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this isn't needed, right? Doesn't the module already export the correct types?

@FloEdelmann FloEdelmann Nov 30, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the react module, the return type of functional components is ReactElement, not JSX.Element (which is of course not actually true for us because not dom-chef is used instead of react). So when I remove the type declaration, I get errors like this:

source/github-widgets/notice-bar.tsx:17:6 - error TS2786: 'XIcon' cannot be used as a JSX component.
  Its return type 'ReactElement<any, any> | null' is not a valid JSX element.
    Type 'ReactElement<any, any>' is not assignable to type 'Element'.

17                      <XIcon/>

@fregante
fregante merged commit 600ecbf into refined-github:master Nov 30, 2020
@fregante

Copy link
Copy Markdown
Member

Yessss 🙌

@FloEdelmann
FloEdelmann deleted the octicons-react branch December 1, 2020 07:36
@yakov116

Copy link
Copy Markdown
Member

image
image

I still have this when building local. Any idea why?

@FloEdelmann

Copy link
Copy Markdown
Member Author

@yakov116 npm i maybe?

@yakov116

Copy link
Copy Markdown
Member

I tried everything I delete node modules, npm update etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants