material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

fontSize style not applied on FontIcon inside a IconButton

Open nzeugaa opened this issue 10 years ago • 15 comments

Problem description

Hello everyone. I have encountered a problem applying fontSize style on FontIcon. FontIcon is inside a IconButton. The icon size is still the default size (24).

Steps to reproduce

import React, { Component } from 'react';
import IconButton from 'material-ui/IconButton';
import FontIcon from 'material-ui/FontIcon';

export default class Test extends Component {
  getStyles() {
    const styles = {
      iconStyles: {
        fontSize: 16,
      },
    };
    return styles;
  }

  render() {
    const styles = this.getStyles();
    return (
        <IconButton tooltip="Add">
          <FontIcon className="material-icons" style={styles.iconStyles}>add_box</FontIcon>
        </IconButton>
    );
  }
}

Versions

  • Material-UI: ^0.15.2
  • React: ^15.2.1
  • Browser: Chrome

nzeugaa avatar Jul 14 '16 23:07 nzeugaa

I found this looking for something else but I think I can answer your question. If you take a look at the docs for <IconButton /> it looks like you can change the size using the iconStyle property in addition to the style property. Like so.

const styles = {
  smallIcon: {
    width: 36,
    height: 36,
  },
  small: {
    width: 72,
    height: 72,
    padding: 16,
  },
};

const IconButtonExampleSize = () => (
  <div>
    <IconButton
      iconStyle={styles.smallIcon}
      style={styles.small}>
         <ActionHome />
    </IconButton>
  </div>
);

caseychoiniere avatar Jul 29 '16 17:07 caseychoiniere

@caseychoiniere This will works only for SVG icons but not for Font icons. FontIcon has a fontSize attribute but this attribute is ignored when FontIcon is a child of IconButton. Anyway the workaround is to use SVG icons like y've described.

nzeugaa avatar Aug 01 '16 15:08 nzeugaa

+1, this is quite frustrating! Thanks tho' for the amazing job.

jsamr avatar Aug 17 '16 17:08 jsamr

Looks like we can close this issue

aahan96 avatar Aug 17 '16 20:08 aahan96

Has it been fixed?

caseychoiniere avatar Aug 17 '16 20:08 caseychoiniere

I am still having the issue with react 15.3.0 and material-ui 0.15.4

jsamr avatar Aug 17 '16 20:08 jsamr

@aahan96 Please re-open this issue since it's not already fixed.

nzeugaa avatar Aug 18 '16 19:08 nzeugaa

The docs themselves at http://www.material-ui.com/#/components/raised-button list the following code:

import FontIcon from 'material-ui/FontIcon';
<RaisedButton
      href="https://github.com/callemall/material-ui"
      target="_blank"
      secondary={true}
      icon={<FontIcon className="muidocs-icon-custom-github" />}
      style={style}
/>

And yet it doesn't work and there's nothing explaining how to get it to work. The <FontIcon> element seems to not even add the required font-family attribute for icons to work if manually adding them to the head, either.

MaffooBristol avatar Apr 12 '17 19:04 MaffooBristol

Including the font css then makes it work with icon={<i className="material-icons">edit</i>}, however it doesn't inherit the same colour as the text along with it, which is problematic with theming. Adding it directly into the label is not very accessible and causing styling issues.

Basically, the way the docs describe it, this should be super easy and require no extra work, yet there are several threads about this spanning years that have people in it who are completely lost!

MaffooBristol avatar Apr 12 '17 20:04 MaffooBristol

@aahan96 not fixed. Reopen issues, plz.

ts-ign0re avatar May 25 '17 06:05 ts-ign0re

Also voting to reopen the issue. For now I copied the FontIcon component and referenced it in my own project rather than material-ui, and I've just passed getStyles a fontSize prop that it puts in its root styling if it exists, else use the default.

function getStyles(props, context, state) {
    const {color, hoverColor, fontSize} = props;

    const {baseTheme} = context.muiTheme;
    const offColor = color || baseTheme.palette.textColor;
    const onColor = hoverColor || offColor;

    return {
        root: {
            color: state.hovered
                ? onColor
                : offColor,
            position: 'relative',
            fontSize: fontSize || baseTheme.spacing.iconSize,
            display: 'inline-block',
            userSelect: 'none',
            transition: transitions.easeOut()
        }
    };
}

je553 avatar Jun 01 '17 21:06 je553

Not sure why this was ever closed. @aahan96?

Preaching to the choir, but - the best way to get an issue fixed is (after discussing your approach) to submit a PR.

mbrookes avatar Jun 01 '17 22:06 mbrookes

I still agree that FontIcon's style prop should still be the ideal way to set the FontIcon's style inside of a IconButton, however we were able to get the FontIcon size working properly on our project like this:

<IconButton
    onTouchTap={this.handleTouchTap}
    iconStyle={{fontSize: '18px'}}>

        <FontIcon className="fa fa-filter" color={accentBlue}/>

</IconButton>

Material-UI v0.18.0

jgreubel avatar Jun 05 '17 22:06 jgreubel

Thank you @jgreubel! That workaround is rather simple and gets the job done.

chalisegrogan avatar Jul 19 '17 20:07 chalisegrogan

We have been porting the component on the v1-beta branch. We reimplemented it from the ground-up. While we haven't tested it, I think that the issue is most likely fixed on that branch. Hence, I'm closing it. Still, we will accept PR fixes until v1-beta takes over the master branch.

oliviertassinari avatar Jul 28 '17 20:07 oliviertassinari