英文:
tailwind how to display a layout based on breakpoints
问题
我正在尝试显示一个页面的布局,该布局取决于它是否与手机的宽度相符。如果是屏幕宽度,我想显示卡片,否则显示表格。有谁知道如何使用Tailwind的断点来实现这一点吗?到目前为止,我一直在使用window.innerWidth
,但它不起作用,我认为这是一种不好的方法。
这是我目前的代码:
if (window.innerWidth > 600) {
content = <DataTable columns={columns} data={data} />;
} else {
console.log(window.innerWidth);
content = <p>在此插入卡片...</p>;
}
然后当然,我像这样渲染内容:
<>
<div className="text-[35px] text-neutral-900">
欢迎回来,{" "}
<span className="font-semibold text-[50px] text-black">
{currentUser.name}
</span>
</div>
<div className="text-[25px] text-zinc-400 font-extralight">
这是您的提醒...
</div>
{content}
</>
我考虑在达到断点之前使卡片不可见,以便同时渲染两者,但这也似乎不是一个好主意。
英文:
I'm trying to display a layout for a page that's dependent on it being the width of a phone. If it's screen width, I want to display cards else, a table. Does anyone know how to do this using tailwind's breakpoints? Until now I've been using window.innerwidth, but it's not working and I think it's a bad approach.
Here's what I have:
if (window.innerWidth > 600) {
content = <DataTable columns={columns} data={data} />;
} else {
console.log(window.innerWidth);
content = <p>insert cards here...</p>;
}
And then ofc, I'm rendering content like so:
<>
<div className="text-[35px] text-neutral-900">
Welcome back,{" "}
<span className="font-semibold text-[50px] text-black">
{currentUser.name}
</span>
</div>
<div className="text-[25px] text-zinc-400 font-extralight">
Here are your reminders...
</div>
{content}
</>
I was thinking making the the cards invisible until it reaches a breakpoint, so rendering both, but that also doesn't seem like a good idea.
答案1
得分: 2
是的,Tailwind具有与CSS媒体查询相同的断点。
但是Tailwind的断点是预定义的,例如:sm表示640px,您可以在它们的文档中查看所有断点及其值这里。您可以在此部分中覆盖默认值。
此外,我注意到您正在使用任意值,如text-[35px]
。Tailwind鼓励每个人使用它们预定义的值,例如:text-4xl
。这样做是为了保持界面的一致性。
英文:
Yes tailwind has breakpoints same as CSS media queries.
But the tailwind breakpoints are pre-defined eg: sm is 640px you can have a look at all breakpoints and their values in their docs here. You can Override the defaults as mentioined in this section
Also I saw you are using arbitrary values like text-[35px]
. Tailwind encourages everyone to use their pre-defined values eg: text-4xl
. This is done to maintain UI consistency.
答案2
得分: 1
我建议使用react-use来获取CSS媒体查询。
import { useMedia } from 'react-use';
const App = () => {
const isWide = useMedia('(min-width: 600px)');
const content = isWide ? (
<DataTable columns={columns} data={data} />
) : (
<p>在这里插入卡片...</p>
);
return (
<>
{/* YOUR CODE */}
{content}
</>
);
};
你可以从其文档中获取更多信息。
如果你不喜欢这个钩子,那么除了渲染两个元素之外别无他法。
const content = (
<>
<div className='hidden sm:block'>
<DataTable columns={columns} data={data} />
</div>
<div className='block sm:hidden'>
<p>在这里插入卡片...</p>
</div>
</>
);
在添加上述内容之前,你需要在tailwind.config.js
中覆盖默认的屏幕断点。
module.exports = {
theme: {
screens: {
'sm': '640px',
// => @media (min-width: 600px) { ... }
}
}
};
在这里查看更多信息。配置自定义屏幕
英文:
I recommend react-use for getting a CSS media query.
import {useMedia} from 'react-use';
const App = () => {
const isWide = useMedia('(min-width: 600px)');
const content = isWide ? (
<DataTable columns={columns} data={data} />
) : (
<p>insert cards here...</p>
)
return (
<>
{/* YOUR CODE */}
{content}
</>
);
};
You can get more idea from its documentation
If you don't like this hook, then there is no other way than rendering both elements.
const content = (
<>
<div className='hidden sm:block'>
<DataTable columns={columns} data={data} />
</div>
<div className='block sm:hidden'>
<p>insert cards here...</p>
</div>
</>
)
Before adding above, you need to override default screen breakpoints at tailwind.config.js.
module.exports = {
theme: {
screens: {
'sm': '640px',
// => @media (min-width: 600px) { ... }
}
}
}
Take a look at here. Configuring custom screens
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论