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

Suggestion: Default Export #38

Closed
a-y-u-s-h opened this issue Oct 22, 2020 · 1 comment
Closed

Suggestion: Default Export #38

a-y-u-s-h opened this issue Oct 22, 2020 · 1 comment

Comments

@a-y-u-s-h
Copy link

a-y-u-s-h commented Oct 22, 2020

Hi, I love the icons. To change them faster in the application I created this component, which could be default export of the application maybe. It simply returns the appropriate component after searching in all exports.

import * as Icons from "@modulz/radix-icons"

export const Icon = React.forwardRef(({ children, name, ...props }, ref) => {
  if (name) {
    const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1)
    const modified = name
      .split(" ")
      .map(e => capitalize(e))
      .join("")
    const id = `${modified}Icon`
    const available = Object.keys(Icons)
    if (available.includes(id)) {
      const Icon_ = Icons[id]
      return (
        <Icon_ {...props} ref={ref}>
          {children}
        </Icon_>
      )
    }
  }
  return <></>
})

export default Icon

Usage:

<Icon name="layers" />
<Icon name="zoom in" />
@vladmoroz
Copy link
Collaborator

Hey, thanks for the suggestion! I have also experienced that it might feel a bit cumbersome to change out the imported icons quickly.

I’d recommend this import style if you want a quick developer experience:

import * as Icons from "@modulz/radix-icons"

function MyComponent () { 
  return <Icons.LayersIcon />
}

This enables code editor autocompletion as you type the icon name and will work out better with TypeScript in case we introduce size or variant props for certain icons.

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

No branches or pull requests

2 participants