question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Carbon 11 Button's renderIcon does not position correctly for functions

See original GitHub issue

Package

@carbon/react

Browser

Firefox

Package version

v11

React version

v18

Description

With Carbon 11, you can pass in a size to an icon such that instead of doing this in v10

import { Add24 } from '@carbon/icons-react';

you can do this in v11

import { Add } from '@carbon/react/icons';
...
<Add size={24} />

So here is my issue, before I could pass a Icon object into the renderIcon of a Button component as such

 <Button renderIcon={Add24} />

But if we wanted to use the new carbon 11 way passing in a size as such, you have to use a function for this to work

 <Button renderIcon={() => <Add size={24}>} />

Now I noticed these two different ways the icon position is off.

Using an object (passing in something like Add24)

image

Passing in a function (() => <Add size={24}>)

image

The pos is off. I know we can just import bot hAdd and Add24 to solvethis , but wanted to ask if I was doing something wrong or if this was ab ug?

CodeSandbox example

N/A

Steps to reproduce

See Description

Code of Conduct

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
TannerScommented, Apr 9, 2022

@TannerS This is due to missing classes on the SVG element, it can be fixed by passing the props to the function component, like this:

renderIcon={props => <Add size={24} {…props}/>}

Yup, this worked. I also learned that, even with the size prop, it doesn’t matter. It appears with a function rendering an icon with a fixed size prop gets ignored and the icon gets set to the size of what the component chooses.

if a function is used, the props appears to be needed as you said.

can this be added in docs somewhere? maybe storybook? I seen others with this issue and no where does it mention passing in props as the functions call back for the renderIcon prop. I feel this would help others, but with that said, my issue has been resolved, thanks @rwibm !

1reaction
rwibmcommented, Apr 9, 2022

@TannerS This is due to missing classes on the SVG element, it can be fixed by passing the props to the function component, like this:

renderIcon={props => <Add size={24} {…props}/>}

Read more comments on GitHub >

github_iconTop Results From Across the Web

When using latest carbon-icons, ghost button does not render ...
Hi, we updated to the latest carbon components / icons, and we're having problems with our ghost button icons, the background color seems...
Read more >
Icon usage - Carbon Design System
Icons are visual symbols used to represent ideas, objects, or actions. They communicate messages at a glance, afford interactivity, and draw attention to ......
Read more >
office-ui-fabric-react/CHANGELOG.md - UNPKG
28, - Dropdown: Fix bug where it was not programatically possible to focus on Dropdown ... 197, - DetailsList: Assign button role to...
Read more >
Display Bug: Buttons with Icon and/or Menu - Mockups 3
I discovered a display bug in the button control when used with an icon and/or a menu after importing some symbols I'd made...
Read more >
In React ES6, why does the input field lose focus after typing a ...
it is because you are rendering the form in a function inside render(). Every time your state/prop change, the function returns a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found