Skip to content

Dynamically import icon svgs instead of building them into bundles#1822

Closed
chandlerprall wants to merge 15 commits intoelastic:masterfrom
chandlerprall:feature/icon-breakapart
Closed

Dynamically import icon svgs instead of building them into bundles#1822
chandlerprall wants to merge 15 commits intoelastic:masterfrom
chandlerprall:feature/icon-breakapart

Conversation

@chandlerprall
Copy link
Copy Markdown
Contributor

@chandlerprall chandlerprall commented Apr 11, 2019

Summary

Changes EuiIcon to dynamically load SVGs. This has been tested in Kibana and confirmed to work with the webpack setup there. EuiIcon's tests themselves still snapshot the full, rendered, SVG contents. Other snapshots capture the loading state (as will be the case with downstream tests in Kibana, this should not affect Cloud UI as they already mock EuiIcon).

Note this requires manually running the yarn compile-icons command for any new icon or modifications to an existing one, replacing the manual clearing of jest cache.

Caroline is aware of a bug in FF around the loading animation

Checklist

  • This was checked in mobile
  • This was checked in IE11
  • This was checked in dark mode
    - [ ] Any props added have proper autodocs
  • Documentation examples were added
  • A changelog entry exists and is marked appropriately
  • This was checked for breaking changes and labeled appropriately
  • Jest tests were updated or added to match the most common scenarios
    - [ ] This was checked against keyboard-only and screenreader scenarios
    - [ ] This required updates to Framer X components

@cchaos
Copy link
Copy Markdown
Contributor

cchaos commented Apr 12, 2019

@chandlerprall So the problem I'm running into is that I can't transition between the loading state with
css transition because even though it looks like it's just tied to a class swap, when the icon is fully loaded it fully swaps out the SVG instance.

So we could create a different div that sits beneath the SVG, but then we have this extra lingering div and have to deal with absolute positioning. Can you think of another option?

@chandlerprall
Copy link
Copy Markdown
Contributor Author

@cchaos I can add a small delay to removing the loading css class, which should allow any css transitions to apply. Want to try that?

@cchaos
Copy link
Copy Markdown
Contributor

cchaos commented Apr 15, 2019

My only concern with that is that we're then forcing a minimum time period of no-icon even if it's not necessary.

@cchaos
Copy link
Copy Markdown
Contributor

cchaos commented Apr 15, 2019

PR4U: chandlerprall#8

Here are all the gifs of the different sections working so others can take a glance. I slowed my network down to "Fast 3G" in dev tools.

Glyphs

Colors

Apps

**Even tokens and logos **


The feature icons could even be supplied a color to be the "loading" color

And of course dark mode:

Example of usage with EuiNavDrawer

@chandlerprall chandlerprall marked this pull request as ready for review April 16, 2019 19:10
@chandlerprall
Copy link
Copy Markdown
Contributor Author

added @pugnascotia as a reviewer on the cloud consuming side of things

@cchaos
Copy link
Copy Markdown
Contributor

cchaos commented Apr 16, 2019

@chandlerprall I pushed a fix for FF

@cchaos
Copy link
Copy Markdown
Contributor

cchaos commented Apr 16, 2019

IE seems to be fine it almost waits for all the icons before it renders the page, so I can't see the isLoading state.

@cchaos
Copy link
Copy Markdown
Contributor

cchaos commented Apr 16, 2019

You'll need to rebase with master and update a few icons that were added in the meantime

@chandlerprall
Copy link
Copy Markdown
Contributor Author

Merged with changes on master (4 new icons, some typescript clean ups), and cleaned up loading an external image source.

@thompsongl
Copy link
Copy Markdown
Contributor

This has been tested in Kibana and confirmed to work with the webpack setup there

The @babel/plugin-syntax-dynamic-import plugin will need to be added to Kibana, right?

@chandlerprall
Copy link
Copy Markdown
Contributor Author

chandlerprall commented Apr 18, 2019

The @babel/plugin-syntax-dynamic-import plugin will need to be added to Kibana, right?

Nope! Babel isn't configured to process node_modules, it's purely webpack processing modules & chunks at that point.

@thompsongl
Copy link
Copy Markdown
Contributor

Nope!

Verified. However, the addition of the dynamic import babel plugin means that running Kibana locally with a yarn linked EUI will fail (unless "node_modules" is in the path to your EUI repo, which would be really odd)

@pugnascotia
Copy link
Copy Markdown
Contributor

I had to do the following to the source (and then build) to get it to work with Cloud UI:

diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx
index 2b8b21f4..4d456b68 100644
--- a/src/components/icon/icon.tsx
+++ b/src/components/icon/icon.tsx
@@ -396,7 +396,8 @@ export class EuiIcon extends Component<Props, State> {

     if (isIconType(this.props.type)) {
       isLoading = true;
-      import(`./assets/${typeToPathMap[this.props.type]}.js`).then(
+      // tslint:disable-next-line
+      import('./assets/' + typeToPathMap[this.props.type] + '.js').then(
         ({ icon }) => {
           this.setState({
             icon,

It's to do with how Babel transpiles template strings and Webpack contexts. With the above, icons are loaded from their own webpack chunks.

@chandlerprall chandlerprall changed the title [WIP] Dynamically import icon svgs instead of building them into bundles Dynamically import icon svgs instead of building them into bundles Apr 18, 2019
Copy link
Copy Markdown
Contributor

@snide snide left a comment

Choose a reason for hiding this comment

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

I did a quick check against the functionality we need. Custom images work the way I expect. Before I go into a deeper review, we definitely will need to add the following in here:

  1. An example showing custom svg use.
  2. Prop docs explanations.
  3. Some snippets would likely help.
  4. Docs to explain how this work as far as theming. Specifically thinking we'll need to document the class names we use for fills.
  5. Likely some rewrite of our instructions on the top of the page now that this stuff is all different.

If you'd like, I can do a PR for you to handle this. Might have some questions.

@chandlerprall
Copy link
Copy Markdown
Contributor Author

If you'd like, I can do a PR for you to handle this. Might have some questions.

@snide I'd appreciate it a lot :)

@chandlerprall
Copy link
Copy Markdown
Contributor Author

closing in favor of creating a larger feature branch & PR against that

@chandlerprall chandlerprall mentioned this pull request Apr 18, 2019
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants