我在React中做了一个应用程序,并使用了Grommet库来设计我的组件。我定义了一个主题文件来为不同的移动设备设置断点:
const customBreakpoints = deepMerge(grommet, {
global: {
breakpoints: {
small: {
value: 768,
borderSize: {
xsmall: "1px",
small: "2px",
medium: "4px",
large: "6px",
xlarge: "12px"
},
edgeSize: {
none: "0px",
hair: "1px",
xxsmall: "2px",
xsmall: "3px",
small: "6px",
medium: "12px",
large: "24px",
xlarge: "48px"
},
size: {
xxsmall: "24px",
xsmall: "48px",
small: "96px",
medium: "192px",
large: "384px",
xlarge: "768px",
full: "100%"
}
},
medium: { value: 1536 },
large: {}
}
}
});
要布局按钮,我使用了扣眼框组件:
const App = () => (
<Grommet theme={customBreakpoints}>
<ResponsiveContext.Consumer>
{size => (
<div>
<Box
direction="column"
align="center"
gap="medium"
pad="small"
overflow={{
horizontal: "auto"
}}
>
<Button
primary
hoverIndicator="true"
style={{
width: "100%"
}}
label="Next"
/>
<Box width="medium" direction="row-responsive">
<Button
primary
icon={<DocumentPdf color="white" />}
style={{
boxSizing: "border-box",
background: "red",
height: "38px",
lineHeight: "24px",
fontSize: "18px",
fontWeight: 600,
paddingLeft: "20px",
paddingRight: "20px"
}}
label="Export PDF"
/>
<Button
primary
icon={<DocumentPdf color="white" />}
style={{
boxSizing: "border-box",
background: "red",
height: "38px",
lineHeight: "24px",
fontSize: "18px",
fontWeight: 600,
paddingLeft: "20px",
paddingRight: "20px"
}}
label="Export all"
/>
</Box>
</Box>
</div>
)}
</ResponsiveContext.Consumer>
</Grommet>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
当我运行应用程序并在不同设备上检查当前窗口时,我得到以下输出:
和
即使使用ResponsiveContext.Consumer并设置 width=size} 它也不起作用。
有什么建议么?
我做了一个沙盒示例。