gpt4 book ai didi

javascript - 为样式组件创建响应式 Prop

转载 作者:行者123 更新时间:2023-12-01 00:30:34 25 4
gpt4 key购买 nike

我正在尝试为样式组件创建响应式 Prop ,如下所示。首先,我们有一个组件(假设是一个按钮):

<Button primary large>Click Me</Button>

此按钮将获得主要背景色和大尺寸(由主题文件确定)。

我现在想创建此按钮的响应版本。这就是我希望的工作方式:

<Button 
primary
large
mobile={{size: 'small', style: 'secondary'}}
tablet={size: 'small'}}
widescreen={{style: 'accent'}}
>
Click Me
</Button>

我现在有了相同的按钮,但样式和尺寸因不同的屏幕尺寸而异。

现在,我已经让它工作了——但它涉及到很多重复的代码。这是一个示例:

const Button = styled('button')(
({
mobile,
tablet,
tabletOnly,
desktop,
widescreen
}) => css`

${mobile &&
css`
@media screen and (max-width: ${theme.breakpoints.mobile.max}) {
background-color: ${colors[mobile.style] || mobile.style};
border: ${colors[mobile.style] || mobile.style};
border-radius: ${radii[mobile.radius] || mobile.radius};
color: ${mobile.style && rc(colors[mobile.style] || mobile.style)};

}
`}

${tablet &&
css`
@media screen and (min-width: ${theme.breakpoints.tablet.min}), print {
background-color: ${colors[tablet.style] || tablet.style};
border: ${colors[tablet.style] || tablet.style};
border-radius: ${radii[tablet.radius] || tablet.radius};
color: ${tablet.style && rc(colors[tablet.style] || tablet.style)};
}
`}

${tabletOnly &&
css`
@media screen and (min-width: ${theme.breakpoints.mobile.min}) and (max-width: ${theme.breakpoints.tablet.max}) {
background-color: ${colors[tabletOnly.style] || tabletOnly.style};
border: ${colors[tabletOnly.style] || tabletOnly.style};
border-radius: ${radii[tabletOnly.radius] || tabletOnly.radius};
color: ${tabletOnly.style &&
rc(colors[tabletOnly.style] || tabletOnly.style)};
}
`}
`

我正在寻找一种简化此代码的方法。基本上,我只想编写一次 CSS 样式,然后根据查询对象生成不同的 Prop 和媒体查询,如下所示:

const mediaQueries = {
mobile: {
min: '0px',
max: '768px'
},
tablet: {
print: true,
min: '769px',
max: '1023px'
},
desktop: {
min: '1024px',
max: '1215px'
},
widescreen: {
min: '1216px',
max: '1407px'
},
fullhd: {
min: '1408px',
max: null
}
}

我想我应该能够创建一个循环遍历 mediaQueries 对象并为每次迭代插入适当的 css 的函数。但是,我似乎不知道如何做到这一点。

关于如何做到这一点有什么想法吗?

另外,提前感谢您提供的任何帮助。

最佳答案

也许您正在寻找这样的东西:

import { css } from "styled-components";

//mobile first approach min-width
const screenSizes = {
  fullhd: 1408,
widescreen: 1215,
  desktop: 1023,
  tablet: 768,
  mobile: 0
}
const media = Object
    .keys(screenSizes)
    .reduce((acc, label) => {
        acc[label] = (...args) => css`
            @media (min-width: ${screenSizes[label] / 16}rem) {
                ${css(...args)}
            }
        `
        return acc
    }, {});

然后你只需像这样导入和使用:

import media from './media'
const button = styled.button`
${({large , small})=> media.mobile`
color: red;
font-size: ${large ? '2em' : '1em'};
`}
`

这里有一些进一步的阅读,包括使用主题:

Media queries in styled-components

使用 Prop :

使用与上面相同的媒体查询对象:

创建一个辅助函数以将样式对象格式化为 CSS 字符串:

const formatCss = (styleObject) => {
return JSON.stringify(styleObject)
.replace(/[{}"']/g,'')
.replace(/,/g,';')
+ ';'
}

创建另一个辅助函数来映射样式并通过映射其键并使用括号表示法动态添加查询来生成查询:

const mapQueries = (myQueries) =>{
return Object.keys(myQueries).map(key=> media[key]`
${formatCss(myQueries[key])}
`)
}

在您的样式组件中:

export const Button = styled.button`
${({myQueries}) => !myQueries ? '' : mapQueries(myQueries)}
`

最后将 myQueries 属性添加到您的组件中,如下所示(为了简单起见,请注意使用 css-formatted 键而不是 javascriptFormatted 键):

<Button myQueries={{
mobile:{ color:'red' },
tablet:{ color:'blue', "background-color":'green'},
desktop:{ height:'10rem' , width:'100%'}
}}>Button</Button>

关于javascript - 为样式组件创建响应式 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58581146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com