Fragment、Portal、そしてDOM順序の落とし穴
描画順序とアクセシビリティツリーの順序を一致させます。
「Fragment、Portal、そしてDOM順序の落とし穴」はCoddyKit上の無料Web Accessibility Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb Accessibility Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web Accessibility Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Source Order Is the Default Order
Screen readers and the keyboard follow the DOM order, the order elements appear in the document. CSS may move things visually, but reading order does not change.
Fragments Add No Extra Box
A React Fragment groups children without adding a wrapper div. That keeps your DOM clean and avoids breaking lists or tables with stray elements.
<>
<th>Name</th>
<th>Email</th>
</>Why a Wrapper div Can Hurt
An extra div inside a list or table can break the parent-child structure assistive tech expects. A Fragment avoids that invalid nesting entirely.
Portals Render Elsewhere
A Portal renders children into a different DOM node, often the document body. It is the standard way to mount modals above everything else.
createPortal(<Modal />, document.body);Portals Keep React Context
Even though the markup lands elsewhere, a portal stays in your component tree. State and context flow normally, so logic is unaffected.
The DOM Order Trap
A portal at the end of body can appear after unrelated content. Tabbing may jump to a far-off spot, so focus management becomes essential for portals.
Move Focus Into the Portal
When a portal opens, the keyboard does not follow automatically. Explicitly move focus into the modal so the user is where the new content is.
Trap and Return Focus
While a portal modal is open, keep Tab inside it. On close, return focus to the element that opened it, since DOM proximity will not do this for you.
Visual Order Versus Reading Order
CSS flexbox order or grid placement can rearrange the screen. The tab order still follows the source, so avoid reordering interactive content with CSS.
Keep Source Order Meaningful
Write your JSX so the source order matches the logical reading flow. Then both sighted and screen reader users encounter content in the same sensible sequence.
Test by Tabbing
Press Tab through your app and watch where focus goes. If it jumps somewhere unexpected, your DOM order or a portal needs attention.
Quick Check
You mount a modal with a portal into document.body. What new responsibility does that create?
Recap: Order and Portals
Use Fragments to avoid invalid wrappers, keep source order logical, and manage focus carefully whenever a portal renders content elsewhere. 🧩
よくある質問
「Fragment、Portal、そしてDOM順序の落とし穴」レッスンは無料ですか?
はい。「Fragment、Portal、そしてDOM順序の落とし穴」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web Accessibility Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web Accessibility Academyコースには全4レッスンが含まれています。
「Fragment、Portal、そしてDOM順序の落とし穴」で何を学びますか?
描画順序とアクセシビリティツリーの順序を一致させます。 ブラウザで直接実行するハンズオンコードでWeb Accessibility Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Web Accessibility Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのWeb Accessibility Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Fragment、Portal、そしてDOM順序の落とし穴」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このWeb Accessibility Academyレッスンでコードを書いて実行できますか?
はい。すべてのWeb Accessibility Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ページを再読み込みせずにルートを変更する
- RefsとuseEffectでフォーカスを管理する
- Fragment、Portal、そしてDOM順序の落とし穴
- jsx-a11yとコンポーネントレベルのガードレール