/ Web Platform
Popover, Anchor, Select: When the Browser Builds Your UI
Những API giao diện mới của trình duyệt có thể thay thế một phần JavaScript tự viết như thế nào.How newer browser UI APIs can replace some hand-written JavaScript.

Nguồn tham khảo: AnhTu.devReference source: AnhTu.dev
Trước đây mình thường nghĩ menu nổi hoặc dropdown đẹp đều cần khá nhiều JavaScript. Các API mới như Popover và CSS Anchor Positioning cho trình duyệt xử lý nhiều phần khó sẵn có.
Popover dùng khi nào?
Popover phù hợp với menu, bảng gợi ý hoặc nội dung tạm thời. Trình duyệt hỗ trợ lớp hiển thị trên cùng, đóng khi bấm ra ngoài và một phần hành vi bàn phím. Với tác vụ bắt buộc người dùng hoàn thành, dialog vẫn phù hợp hơn.
Đặt vị trí bằng anchor
Anchor Positioning giúp đặt một phần tử cạnh nút mở nó mà không phải liên tục đo tọa độ bằng JavaScript. Mình vẫn cần kiểm tra màn hình nhỏ, zoom lớn và trường hợp không đủ chỗ.
Cách mình sẽ dùng
- Giữ HTML cơ bản hoạt động trước.
- Chỉ thêm tính năng mới khi trình duyệt hỗ trợ.
- Kiểm tra Escape, focus và screen reader.
- Không ép native select làm những việc quá phức tạp.
Bài học với mình là nên thử khả năng của trình duyệt trước khi cài thêm thư viện hoặc tự viết một component lớn.
Popover and CSS Anchor Positioning let the browser handle UI behaviour that previously needed a lot of JavaScript.
Use the right primitive
Popover works for temporary menus and suggestions. Dialog is still the better choice when the user must finish or cancel a task.
Anchor positioning can place a surface next to its trigger, but small screens, zoom, and fallback positions still need testing.
- Keep baseline HTML usable.
- Enhance only when supported.
- Test Escape, focus, and screen readers.
- Do not force native select into an unsuitable role.
My takeaway is to check what the browser can already do before adding another library.