Blades CSS v3.1.1 发布:无需 JS 即可实现 dialog 动画
来源标题:Show HN: No-JS <dialog> in Blades CSS v3.1.1 (a Pico CSS fork)
Blades CSS v3.1.1(Pico CSS 的分支)支持纯 CSS 实现 dialog 淡入和 article 缩放动画,无需 JS 类。借助 Invoker Commands API,仅用属性即可打开和关闭 dialog,Pico 原有的模态 JS 仍兼容。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
用 dialog.showModal() 打开的 <dialog> 会淡入,其中的 <article> 会缩放进入,全程不需要 JS 类。
Pico 原有的模态 JS(.modal-is-open、.modal-is-opening、.modal-is-closing)依然可以在其之上正常工作。
dialog:modal {
transition: opacity var(--pico-transition);
> article {
transition: transform var(--pico-transition);
}
dialog:modal {
opacity: 0;
> article {
transform: scale(0.96);
}
}
}
无需 JS 的打开与关闭(新增)
Invoker Commands API 让 <button> 仅凭属性就能打开和关闭 dialog。给按钮一个 command,再用 commandfor 指向 dialog 的 id:
<button command="show-modal" commandfor="my-dialog">Open</button>
<dialog id="my-dialog" closedby="any">
<article>
<header>
<button aria-label="Close" rel="prev" command="close" commandfor="my-dialog"></button>
<h3>Title</h3>
</header>
<p>Content.</p>
<footer>
<button class="secondary" command="request-close" commandfor="my-dialog">Cancel</button>
<button command="close" commandfor="my-dialog" value="confirm">Confirm</button>
</footer>
</article>
</dialog>
show-modal 等同于 dialog.showModal(),因此上面的过渡效果同样生效。
close 等同于 dialog.close()。它会立即关闭,按钮的 value 会成为 dialog.returnValue。
request-close 会像按下 Escape 一样先触发 cancel 事件,因此监听器可以用 preventDefault() 阻止关闭。
按钮必须是 <button>(或 <input type="button">),不能放在会触发提交的 <form> 里,除非它带有 type="button"。
dialog 会收到一个 command 事件,event.source 就是触发它的按钮。
不支持该特性的浏览器会忽略这些属性,什么也不会打开。如果需要,可以加一个 click 监听器调用 showModal() 和 close() 作为兜底。
点击外部关闭(新增)
<dialog closedby="any"> 会在用户点击其外部区域时关闭。这由浏览器完成,点击本身不需要 JS。
遮罩的变暗和模糊效果位于 ::backdrop 上,并在 --pico-transition 期间淡入。
不带 closedby="any" 的 dialog 行为不变。
/anyblades/blades/refs/heads/main/src/blades/components/_modal-modern.css:
dialog:modal[closedby="any"] {
width: calc(100% - var(--pico-spacing) * 2);
max-width: none;
min-width: 0;
height: fit-content;
min-height: 0;
max-height: calc(100% - var(--pico-spacing) * 2);
margin: auto;
padding: 0;
background: transparent;
-webkit-backdrop-filter: none;
backdrop-filter: none;
> article {
width: 100%;
max-width: none;
max-height: calc(100vh - var(--pico-spacing) * 2);
max-height: calc(100dvh - var(--pico-spacing) * 2);
margin: 0;
}
dialog:modal[closedby="any"] {
width: min(510px, calc(100% - var(--pico-spacing) * 2));
}
dialog:modal[closedby="any"] {
width: min(700px, calc(100% - var(--pico-spacing) * 2));
}
}
dialog:modal[closedby="any"]::backdrop {
background: var(--pico-modal-overlay-background-color);
-webkit-backdrop-filter: var(--pico-modal-overlay-backdrop-filter);
backdrop-filter: var(--pico-modal-overlay-backdrop-filter);
transition:
background-color var(--pico-transition),
-webkit-backdrop-filter var(--pico-transition),
backdrop-filter var(--pico-transition);
dialog:modal[closedby="any"]::backdrop {
background-color: transparent;
-webkit-backdrop-filter: none;
backdrop-filter: none;
}
dialog:modal[closedby="any"]::backdrop {
transition: none;
}
}
语法
这是经典的模态组件,在不同设备和视口下拥有优雅的间距,使用语义化 HTML 标签 <dialog>。
模态以 <dialog> 作为外层容器,以 <article> 承载模态内容。
在 <header> 内,<button rel="prev"> 被定义为 float: right;,让关闭图标与标题顶部对齐。
我们很高兴你参加即将到来的活动。请准时到达博物馆签到并开始活动。
<dialog open>
<article>
<header>
<button aria-label="Close" rel="prev"></button>
<p>
<strong>🗓️ Thank You for Registering!</strong>
</p>
</header>
<p>
We're excited to have you join us for our
upcoming event. Please arrive at the museum
on time to check in and get started.
</p>
<ul>
<li>Date: Saturday, April 15</li>
<li>Time: 10:00am - 12:00pm</li>
</ul>
</article>
</dialog>
在 <footer> 内,内容默认右对齐。
感谢你注册会员!请查看下方的会员详情:
<dialog open>
<article>
<h2>Confirm Your Membership</h2>
<p>
Thank you for signing up for a membership!
Please review the membership details below:
</p>
<ul>
<li>Membership: Individual</li>
<li>Price: $10</li>
</ul>
<footer>
<button class="secondary">
Cancel
</button>
<button>Confirm</button>
</footer>
</article>
</dialog>
演示
点击下方按钮切换模态。
Pico 不包含 JavaScript 代码。你需要自行实现 JS 来与模态交互。
作为起点,你可以查看 HTMLDialogElement 或下面的进阶示例:
要打开模态,给 <dialog> 容器添加 open 属性。
这些类在无类版本中不可用。
.modal-is-open 阻止模态下方的一切滚动和交互。
<!doctype html>
<html class="modal-is-open">
...
</html>
.modal-is-opening 带来打开动画。
<!doctype html>
<html class="modal-is-open modal-is-opening">
...
</html>
.modal-is-closing 带来关闭动画。
<!doctype html>
<html class="modal-is-open modal-is-closing">
...
</html>
在 GitHub 上编辑此页
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。