在React中,单击按钮后,在侧边栏右侧显示一个组件,而不改变布局。

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

In react, display a component on the right of sidebar after clicking button without changing layout

问题

我想要在点击“+”按钮后显示如图中所示的组件。这样的组件应该位于侧边栏的右侧,覆盖主要部分。

先前的布局是一个2列网格,不会改变,只是在其他组件上创建了一个新组件。

我困惑的是如何设置布局(例如,使用tailwind)以及放置新组件的正确方式(最好将其作为侧边栏的子组件,还是将侧边栏、主要部分和新组件置于同一级别?)

英文:

在React中,单击按钮后,在侧边栏右侧显示一个组件,而不改变布局。

I would like to display such a component in the picture after clicking the "+" button. such a component should be at the right of the sidebar and over the main section.

The previous layout is a 2-col-grid and will not be changed, just a new component created over the others.

What I am confused is how to set the layout (e.g. using tailwind) and what is the proper way to place code of the new component (is it better to make it a child component of the sidebar, or make sidebar, main, and the new component the same level?)

答案1

得分: 1

关于要使用的方法,我通常选择在 className 内使用三元运算符,例如:

const [isClicked, setIsClicked] = useState(false)
<Sidebar>
   <div className={`${isClicked ? 'opacity-1 pointer-events-auto' : 'opacity-0 pointer-events-none'}`}></div>
</Sidebar>

如果需要的话,最终我可以使用 z-index。我像你建议的那样使用了Tailwind类,但你也可以添加一些自定义样式并用CSS进行样式设置。

英文:

Well, as far as I understand where you put it is up to you. Try to be as semantic as you can with your HTML.

About the method to use, I usually go for a ternary inside className, for example:

const [isClicked, setIsClicked] = useState(false)
&lt;Sidebar&gt;
   &lt;div className={`${isClicked ? &#39;opacity-1 pointer-events-auto&#39; : &#39;opacity-0 pointer-events-none&#39;}`}&gt;&lt;/div&gt;
&lt;/Sidebar&gt;

Eventually I could make use of z-index if needed. I used tailwind classes as you suggest, but you can add some custom and style it with CSS.

huangapple
  • 本文由 发表于 2023年3月4日 00:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629602.html
匿名

发表评论

匿名网友

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

确定