web-dev-qa-db-fra.com

Comment agrandir l'icône SVG dans iconButtons material-ui?

Quelqu'un a-t-il créé des pages Web à l'aide de react.js et de la bibliothèque Material UI ? Comment dois-je redimensionner la taille de l'icône? C’est une icône svg . Je viens de construire un composant "créer un nouveau", qui est un morceau de papier matériel avec un bouton d’ajout de contenu en haut. Voici le code.

import React from 'react';
import Paper from 'material-ui/lib/paper';
import ContentAdd from 'material-ui/lib/svg-icons/content/add';
import IconButton from 'material-ui/lib/icon-button';


const styleForPaper = {
  width: '96vw',
  height: '20vh',
  margin: 20,
  textAlign: 'center',
  display: 'inline-block',
};

const styleForButton = {
  'marginTop': '7vh',
};


const PaperToAddNewWidgets = () => (
  <div>

    <Paper style={styleForPaper} zDepth={2}>

    <IconButton
        style={styleForButton}
        touch={true}
        tooltip="Add New Widget">

    <ContentAdd/>

    </IconButton>

    </Paper>
  </div>
);

export default PaperToAddNewWidgets;

Cela semble OK (assurez-vous de le visualiser en taille réelle), mais l'icône est trop petite. Ensuite, j'ai ouvert l'outil de développement chrome et vu le code html suivant. 

<div style="background-color:#ffffff;transition:all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;box-sizing:border-box;font-family:Roboto, sans-serif;-webkit-tap-highlight-color:rgba(0,0,0,0);box-shadow:0 3px 10px rgba(0,0,0,0.16),
         0 3px 10px rgba(0,0,0,0.23);border-radius:2px;width:96vw;height:20vh;margin:20px;text-align:center;display:inline-block;mui-prepared:;" data-reactid=".0.2.0.1.0"><button style="border:10px;background:none;box-sizing:border-box;display:inline-block;font:inherit;font-family:Roboto, sans-serif;tap-highlight-color:rgba(0, 0, 0, 0);cursor:pointer;text-decoration:none;outline:none;transform:translate3d(0, 0, 0);position:relative;transition:all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;padding:12px;width:48px;height:48px;font-size:0;margin-top:7vh;mui-prepared:;-webkit-appearance:button;" tabindex="0" type="button" data-reactid=".0.2.0.1.0.0"><div data-reactid=".0.2.0.1.0.0.0"><span style="height:100%;width:100%;position:absolute;top:0;left:0;overflow:hidden;mui-prepared:;" data-reactid=".0.2.0.1.0.0.0.0"></span><div style="position: absolute; font-family: Roboto, sans-serif; font-size: 14px; line-height: 32px; padding: 0px 16px; z-index: 3000; color: rgb(255, 255, 255); overflow: hidden; top: -10000px; border-radius: 2px; opacity: 0; left: -44px; transition: top 0ms cubic-bezier(0.23, 1, 0.32, 1) 450ms, transform 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms, opacity 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; box-sizing: border-box; -webkit-user-select: none;" data-reactid=".0.2.0.1.0.0.0.1:0"><div style="position: absolute; left: 50%; top: 0px; transform: translate(-50%, -50%); border-radius: 50%; transition: width 0ms cubic-bezier(0.23, 1, 0.32, 1) 450ms, height 0ms cubic-bezier(0.23, 1, 0.32, 1) 450ms, backgroundColor 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; width: 0px; height: 0px; background-color: transparent;" data-reactid=".0.2.0.1.0.0.0.1:0.0"></div><span style="position:relative;white-space:nowrap;mui-prepared:;" data-reactid=".0.2.0.1.0.0.0.1:0.1">Add New Widget</span></div><svg style="display:inline-block;height:24px;width:24px;transition:all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;fill:rgba(0, 0, 0, 0.87);mui-prepared:;-webkit-user-select:none;" viewBox="0 0 24 24" data-reactid=".0.2.0.1.0.0.0.1:2:$/=10"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" data-reactid=".0.2.0.1.0.0.0.1:2:$/=10.0"></path></svg></div></button></div>

À l'aide de chrome dev tool, j'ai révisé la taille de l'icône svg et la propriété viewbox de svg et agrandi l'icône dans le navigateur. Mais je ne sais pas comment je peux redimensionner la taille de l'icône dans mon code. Si j'écris un fichier CSS pour réviser le svg, ce sera problématique s'il y a plus d'un élément svg. 

12
Jun Yin

Vous devez définir la taille de l'icône dans la propriété iconStyle dans <IconButton>. Exemple ci-dessous à partir des documents matériel-ui .

D'après mon expérience, si vous définissez uniquement la hauteur ou la largeur, rien ne se produit - semble ne fonctionner que si vous définissez hauteur et largeur sur la même valeur.

import React from 'react';
import IconButton from 'material-ui/IconButton';
import ActionHome from 'material-ui/svg-icons/action/home';

const styles = {

  largeIcon: {
    width: 60,
    height: 60,
  },

};

const IconButtonExampleSize = () => (
  <div>
    <IconButton
      iconStyle={styles.largeIcon}

    >
      <ActionHome />
    </IconButton>
  </div>
);
15
Brandon

fais ça 

<SomeIcon className="svg_icons"/>

.svg_icons{
  transform: scale(1.8);
}

il suffit d'utiliser l'échelle: D ça marche 

Avec la dernière version de material-ui (3.1.0), vous pouvez modifier le remplissage de IconButton et fontSize de SvgIcon pour mettre à jour les apparences.

const styles = theme => ({
  smallButton: {
    padding: 6
  },
  largeButton: {
    padding: 24
  },
  largeIcon: {
    fontSize: "3em"
  },
  input: {
    display: "none"
  }
});    

function IconButtons(props) {
  const { classes } = props;
  return (
    <div>
      <IconButton className={classes.smallButton} aria-label="Delete">
        <DeleteIcon fontSize="small" />
      </IconButton>
      <IconButton aria-label="Delete">
        <DeleteIcon fontSize="large" />
      </IconButton>
      <IconButton className={classes.largeButton} aria-label="Delete">
        <DeleteIcon className={classes.largeIcon} />
      </IconButton>
    </div>
  );
}

Démo en direct

0
Sonic.S.Xiang

J'utilise IconStyle prop pour modifier la taille de l'icône svg.

<IconButton 
   tooltip="Add New Group" 
   tooltipPosition="top-center" 
   style={{padding: 0, height: 0}} 
   iconStyle={{height: 31, width: 48}} 
   onClick={e => this.openNewList()} 
   disabled={!this.state.newAvailable}>

  <ActionHome />

</IconButton>
0
Hemadri Dasari