在我见过的所有的情况下,一个AppBar与工具栏,因为它是第一个孩子实现。工具栏的样式表根据主题中定义的断点指示其高度。
到这里看看:https://github.com/callemall/material-ui/blob/v1-beta/src/Toolbar/Toolbar.js
您可以使用类似的方法来定义一个类样式表为不同的应用断点高度的AppBar图像。然后,在渲染组件时,将该类应用于图像。
注意:如果您使用withStyles HOC(如在工具栏,AppBar等中所做的那样),则该样式表中定义的类将通过名为classes的道具提供。
对于AppBar对组合性的需求是正确的,但该问题尚未解决,而且这是测试分支。解决问题时,应该有一个更好的解决方案,值得向此迁移。
我希望这个答案有帮助。我会添加代码示例,但是同时在一家杂货店的停车场等待我从我的电话接听。如果我有机会,我会更新这个答案。
这里有一个方法,重复的样式在一个新的可重用的组件:
import createStyleSheet from 'material-ui/styles/createStyleSheet';
import withStyles from 'material-ui/styles/withStyles';
// define these styles once, if changes are needed because of a change
// to the material-ui beta branch, the impact is minimal
const styleSheet = createStyleSheet('ToolbarImage', theme => ({
root: {
height: 56,
[`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
height: 48,
},
[theme.breakpoints.up('sm')]: {
height: 64,
},
},
}));
// a reusable component for any image you'd need in a toolbar/appbar
const ToolbarImage = (props) => {
const { src, classes } = this.props;
return (
<img src={src} className={classes.root} />
);
};
// this higher order component uses styleSheet to add
// a classes prop that contains the name of the classes
export default withStyles(styleSheet)(ToolbarImage);
另一种方法是将标准工具栏的高度增加了主题为business variables,覆盖for all Toolbars的root
类,以便它利用他们,并使用主题时,你需要再次引用它们:
// define the standard heights in one place
const toolbarHeights = {
mobilePortrait: 56,
mobileLandscape: 48,
tabletDesktop: 64,
};
// create the theme as you normally would, but add the heights
let theme = createMuiTheme({
palette: createPalette({
primary: blue,
accent: pink,
}),
standards: {
toolbar: {
heights: toolbarHeights,
},
},
});
// recreate the theme, overriding the toolbar's root class
theme = createMuiTheme({
...theme,
overrides: {
MuiToolbar: {
// Name of the styleSheet
root: {
position: 'relative',
display: 'flex',
alignItems: 'center',
minHeight: theme.standards.toolbar.heights.mobilePortrait,
[`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
minHeight: theme.standards.toolbar.heights.mobileLandscape,
},
[theme.breakpoints.up('sm')]: {
minHeight: theme.standards.toolbar.heights.tabletDesktop,
},
},
},
},
});
然后你就可以,因为他们是主题的一部分,您创建的任何样式表中引用这些高度。
修订以下1.0.0-beta.11的发布:
现在有许多关于提供工具栏对了minHeight每个断点的主题可用工具栏混入。如果你需要一个元素相对于AppBar组件的标准高度的风格,你可以用这个对象来建立你自己的风格:
const toolbarRelativeProperties = (property, modifier = value => value) => theme =>
Object.keys(theme.mixins.toolbar).reduce((style, key) => {
const value = theme.mixins.toolbar[key];
if (key === 'minHeight') {
return { ...style, [property]: modifier(value) };
}
if (value.minHeight !== undefined) {
return { ...style, [key]: { [property]: modifier(value.minHeight) } };
}
return style;
}, {});
在这个例子中,toolbarRelativeProperties
返回将返回一个对象,可以函数传播到你的风格对象。它解决了将指定属性设置为基于AppBar高度的值的简单情况。
感谢ken,解释了如何确定'AppBar'的高度(如果我理解正确的话,“ToolBar”的高度)。我猜如果我的目标是将图像添加到“AppBar”中,那么该图像也将是“ToolBar”的一个子项。是否有可能获得对父类'ToolBar''类'对象的引用,所以我可能会从那里引用那些样式,而不是在我的“AppBarImage”组件中指定类似的对象? –
您当然可以将类道具或其中包含的任何类传递给工具栏的任何子节点。问题是,根类比你需要的多。您可能需要使用自己的一组类来定义一个新组件,类似ToolBarImage,您可以在需要将图像放入工具栏中时使用它。 –
感谢ken,是的,我可能不希望将'classes'从'ToolBar'传递到'ToolBarImage',但可能会使用它的一部分。我敢肯定我是一个傻瓜,但我有一个问题,得到一个'工具栏'的'''道具的属性传递给它的一个孩子。我尝试在'ToolBar'上使用'ref'函数,但是当我看着它时,ref没有'classes'道具。 –