tailwind如何根据断点显示布局。

huangapple go评论61阅读模式
英文:

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 &gt; 600) {
  content = &lt;DataTable columns={columns} data={data} /&gt;;
} else {
  console.log(window.innerWidth);
  content = &lt;p&gt;insert cards here...&lt;/p&gt;;
}

And then ofc, I'm rendering content like so:

  &lt;&gt;
    &lt;div className=&quot;text-[35px] text-neutral-900&quot;&gt;
      Welcome back,{&quot; &quot;}
      &lt;span className=&quot;font-semibold text-[50px] text-black&quot;&gt;
        {currentUser.name}
      &lt;/span&gt;
    &lt;/div&gt;
    &lt;div className=&quot;text-[25px] text-zinc-400 font-extralight&quot;&gt;
      Here are your reminders...
    &lt;/div&gt;
    {content}
  &lt;/&gt;

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 &#39;react-use&#39;;

const App = () =&gt; {
  const isWide = useMedia(&#39;(min-width: 600px)&#39;);

  const content = isWide ? (
    &lt;DataTable columns={columns} data={data} /&gt;
  ) : (
    &lt;p&gt;insert cards here...&lt;/p&gt;
  )

  return (
    &lt;&gt;
      {/* YOUR CODE */}
      {content}
    &lt;/&gt;
  );
};

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 = (
  &lt;&gt;
    &lt;div className=&#39;hidden sm:block&#39;&gt;
      &lt;DataTable columns={columns} data={data} /&gt;
    &lt;/div&gt;
    &lt;div className=&#39;block sm:hidden&#39;&gt;
      &lt;p&gt;insert cards here...&lt;/p&gt;
    &lt;/div&gt;
  &lt;/&gt;
)

Before adding above, you need to override default screen breakpoints at tailwind.config.js.

module.exports = {
  theme: {
    screens: {
      &#39;sm&#39;: &#39;640px&#39;,
      // =&gt; @media (min-width: 600px) { ... }
    }
  }
}

Take a look at here. Configuring custom screens

huangapple
  • 本文由 发表于 2023年6月22日 07:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定