在Tailwind SolidJS应用中的模态框定位。

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

Modal positioning in tailwind solidjs app

问题

我有一些关于定位我的模态框的问题。我花了很多时间尝试在其他组件之间滑动我的模态框组件,以获得更好的结果,但现在我真的不知道如何修复它。我有一个注册表单,我想在用户单击下面的按钮时在屏幕中央触发模态框。问题是,模态框不是在中心位置打开的,而是从触发位置打开的。我必须使用绝对类,因为如果不这样做,模态框不会覆盖页面。有什么建议吗?

以下是模态框的代码:

import { Component, createSignal } from "solid-js";

const TermsModal = function () {
  const [isOpen, setIsOpen] = createSignal(false);

  return (
    <div class="flex absolute justify-center">
      <button
        class="text-sm font-bold text-blue-500 uppercase focus:outline-none"
        onClick={() => setIsOpen(true)}
      >
        Privacy Policy
      </button>
      {isOpen() && (
        <>
          <div
            role="presentation"
            class=""
            onClick={() => setIsOpen(false)}
            onKeyPress={(e) =>
              (e.key || e.code) === "Escape" ? setIsOpen(false) : null
            }
          ></div>
          <section
            role="dialog"
            class="p-4 text-center border-gray-200 rounded-lg shadow sm:p-8 bg-zinc-700"
          >
            <button aria-label="Close Dialog" onClick={() => setIsOpen(false)}>
              &times; Close button
            </button>
            {/* Here starts modal content */}
            <h1>Terms of Service</h1>
            {/* Here ends modal content */}
            <p>
              Lorem ipsum dolor sit amet consectetur adipisicing elit. Nisi
              quaerat totam cumque hic voluptatum sit ratione itaque aspernatur,
              possimus ex beatae quo repudiandae dignissimos iure eum
              exercitationem labore, corrupti adipisci.
            </p>
          </section>
        </>
      )}
    </div>
  );
};

export default TermsModal;

这是如何调用模态框的方式。它在输入表单中与复选框一起。(我还希望将它放在一行中,但失败了)

<label class="text-white m-5 p-1">
  <input type="checkbox" checked={false} onChange={handleCheck} />
  &nbsp;I have read and agree to the <TermsModal />{" "}
</label>

你可以尝试使用以上的代码来改进你的模态框定位和触发。希望对你有所帮助。

英文:

i have some issues with positioning my modal. I spend lot of time trying to swipe my modal component between other components to get better result but right now i don't really know how to fix it. I have a signup form and I want to trigger the modal in the center of the screen when user click on the button below. Problem is, modal is not opening in the center, its opening from position it was triggered.
I have to use absolute class because if I wont, modal is not overriding the page. Any suggestions?

Here is the modal code

import { Component, createSignal } from &quot;solid-js&quot;;
const TermsModal = function () {
const [isOpen, setIsOpen] = createSignal(false);
return (
&lt;div class=&quot;flex absolute justify-center&quot;&gt;
&lt;button
class=&quot;text-sm font-bold text-blue-500 uppercase focus:outline-none&quot;
onClick={() =&gt; setIsOpen(true)}
&gt;
Privacy Policy
&lt;/button&gt;
{isOpen() &amp;&amp; (
&lt;&gt;
&lt;div
role=&quot;presentation&quot;
class=&quot;&quot;
onClick={() =&gt; setIsOpen(false)}
onKeyPress={(e) =&gt;
(e.key || e.code) === &quot;Escape&quot; ? setIsOpen(false) : null
}
&gt;&lt;/div&gt;
&lt;section
role=&quot;dialog&quot;
class=&quot; p-4 text-center border-gray-200 rounded-lg shadow sm:p-8 bg-zinc-700&quot;
&gt;
&lt;button aria-label=&quot;Close Dialog&quot; onClick={() =&gt; setIsOpen(false)}&gt;
&amp;times Close button
&lt;/button&gt;
{/* Here starts modal content */}
&lt;h1&gt;Terms of Service&lt;/h1&gt;
{/* Here ends modal content */}
&lt;p&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit. Nisi
quaerat totam cumque hic voluptatum sit ratione itaque aspernatur,
possimus ex beatae quo repudiandae dignissimos iure eum
exercitationem labore, corrupti adipisci.
&lt;/p&gt;
&lt;/section&gt;
&lt;/&gt;
)}
&lt;/div&gt;
);
};
export default TermsModal;

And here is how i am calling a modal. Its inside the input form together with checkbox. (I also want to make it in one line, but failed)

    &lt;label class=&quot;text-white m-5 p-1&quot;&gt;
&lt;input type=&quot;checkbox&quot; checked={false} onChange={handleCheck} /&gt;
&amp;nbsp;I have read and agree to the &lt;TermsModal /&gt;{&quot; &quot;}
&lt;/label&gt;

在Tailwind SolidJS应用中的模态框定位。

在Tailwind SolidJS应用中的模态框定位。

答案1

得分: 2

这可能是因为您将模态窗口呈现为某个元素的子组件。您需要使用Portal来将模态窗口呈现为body元素的直接子元素,以便它不受其父元素的z-index或其他CSS属性的影响。

import { render, Portal } from "solid-js/web";

const style = `
  border: 1px solid red;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-flow: column no-wrap;
  align-items: center;
  justify-content: center;
`

const Component = () => {
  return <div style={style}>Inner Content</div>;
};

const App = () => {
  return <Portal mount={document.body}><Component /></Portal>;
};

render(() => <App />, document.body);

您可以了解更多关于Portal的信息:

如果您不想使用Portal并且不需要在模态窗口内部使用响应性,您可以使用onMount钩子将模态窗口附加到body元素,使用onCleanup来删除它。

import { batch, createSignal, onMount, onCleanup } from "solid-js";
import { render } from "solid-js/web";

const style = `
border: 1px solid red;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
flex-flow: column no-wrap;
align-items: center;
justify-content: center;
`

const Component = () => {
  const modal = document.createElement("div");
  modal.innerHTML = "Some Content";
  modal.setAttribute("style", style);

  onMount(() => {
    document.body.appendChild(modal);
  });

  onCleanup(() => {
    document.body.removeChild(modal);
  });

  return null;
};

const App = () => {
  return <Component />;
};

render(() => <App />, document.body);
英文:

That is probably because you are rendering the modal window as a child component of some element. You need to use Portal to render the modal window as a direct child of the body element so that it won't be affected by its parent's z-index or other css properties.

import { render, Portal } from &quot;solid-js/web&quot;;

const style = `
  border: 1px solid red;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-flow: column no-wrap;
  align-items: center;
  justify-content: center;
`

const Component = () =&gt; {
  return &lt;div style={style}&gt;Inner Content&lt;/div&gt;
};

const App = () =&gt; {
  return &lt;Portal mount={document.body}&gt;&lt;Component /&gt;&lt;/Portal&gt;;
};

render(() =&gt; &lt;App /&gt;, document.body);

https://playground.solidjs.com/anonymous/13e0670c-79e3-4899-8ed0-151dd531e8c2

You can learn more about Portal:

If you don't want to use a portal and don't need reactivity inside the modal window, you can use onMount hook to append the modal window to the body element and onCleanup to remove it.

import { batch, createSignal, onMount, onCleanup } from &quot;solid-js&quot;;
import { render } from &quot;solid-js/web&quot;;

const style = `
border: 1px solid red;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
flex-flow: column no-wrap;
align-items: center;
justify-content: center;
`

const Component = () =&gt; {
  const modal = document.createElement(&quot;div&quot;);
  modal.innerHTML = &quot;Some Content&quot;;
  modal.setAttribute(&quot;style&quot;, style);

  onMount(() =&gt; {
    document.body.appendChild(modal);
  });

  onCleanup(() =&gt; {
    document.body.removeChild(modal);
  });

  return null;
};

const App = () =&gt; {
  return &lt;Component /&gt;;
};

render(() =&gt; &lt;App /&gt;, document.body);

huangapple
  • 本文由 发表于 2023年2月10日 14:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407612.html
匿名

发表评论

匿名网友

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

确定