The book detail modal is written inside BookCard, but it has to show above the whole page. The same title sometimes has to be an h1, sometimes an h2, depending on the choice of the moment. Two built-in Solid components cover these two needs.
<Portal> renders a piece of JSX somewhere else in the DOM, usually straight in document.body, while in your code it stays a child of the component that writes it. <Dynamic> renders a tag or a component that is only picked while the app runs. Think of Nadia writing a promo notice at the cashier's desk, but pinning it on the big board by the entrance, so no shelf hides it. The cashier still wrote it, the board is still where it hangs. Dynamic is like the same book info printed as a big poster or a small label, depending on the choice of the moment.
The problem: a card that clips the modal
The shop's book cards use overflow: hidden, so the cover follows the card's rounded corners. A detail modal written inside the card gets cut off at that card's edge, and no z-index helps, because it's locked inside the card. Modals, toasts, tooltips and dropdowns need to get out of their parent's box. That's the Portal's job.
Portal and mount
import { createSignal, Show } from "solid-js"
import { Portal } from "solid-js/web"
function BookCard(props) {
const [open, setOpen] = createSignal(false)
return (
<div class="card" onClick={() => console.log("card got the click")}>
<h3>{props.title}</h3>
<button onClick={() => setOpen(true)}>See details</button>
<Show when={open()}>
<Portal>
<div class="modal">
<p>{props.title} · $18 · stock 4</p>
<button onClick={() => setOpen(false)}>Close</button>
</div>
</Portal>
</Show>
</div>
)
}
Tested in Solid 1.9.17, after See details is clicked, the DOM:
<body>
<div id="root">
<div class="card"><h3>The Rainbow Troops</h3><button>See details</button></div>
</div>
<div><div class="modal"><p>The Rainbow Troops · $18 · stock 4</p><button>Close</button></div></div>
</body>
Portalis imported from"solid-js/web", not from"solid-js".- With no props, its content goes into
document.body, inside one<div>the Portal makes. The card no longer clips the modal. - The
mountprop picks another place:<Portal mount={document.getElementById("modal-root")}>. - When
Showcloses it, that<div>is taken out ofbodytoo. - Since the modal is no longer a child of
.cardin the DOM, CSS like.card .modal { ... }doesn't apply to it. Give the modal its own class and styles, such asposition: fixed.
Events still follow the component tree
Take a guess first: the <div class="card"> around the Portal has an onClick. You click the text inside the modal. Does the card's onClick run or not?
In the DOM, the modal is a child of body. In your code, it's a child of BookCard. Solid follows the second. Tested in Solid 1.9.17: a click on the text inside the modal runs the onClick on .card and prints card got the click.
The reason: Solid's onClick is delegated, a single listener on document that walks up from the clicked element. When it reaches the Portal's <div>, the walk jumps to the place <Portal> is written, then goes on to .card. A native listener through on:click or addEventListener on .card doesn't hear it, because in the DOM the path is the modal, the <div>, then body. Context gets through too: useContext inside the Portal reads the Provider around the <Portal>.
Dynamic: a tag or component picked while running
The shape of this lesson's exercise:
import { createSignal } from "solid-js"
import { Dynamic } from "solid-js/web"
function App() {
const [tag, setTag] = createSignal("h1")
return (
<div>
<Dynamic component={tag()}>Dynamic Heading</Dynamic>
<button onClick={() => setTag(t => t === "h1" ? "h2" : "h1")}>
Toggle Tag
</button>
</div>
)
}
| Event | tag() |
What shows |
|---|---|---|
| Start | "h1" |
<h1>Dynamic Heading</h1> |
| One click | "h2" |
<h2>Dynamic Heading</h2> |
| Another click | "h1" |
<h1>Dynamic Heading</h1> |
component={tag()}takes a tag name as a string. The text stays, only the kind of element changes.t === "h1" ? "h2" : "h1"makes it flip back and forth: the second click brings it back toh1.
component takes a component too. The shop can show a book as a grid card or a list row:
const views = { grid: CardGrid, row: CardRow }
<Dynamic component={views[mode()]} title="The Rainbow Troops" stock={stock()} />
The other props, title and stock, are passed on to the chosen component and stay reactive. Tested in Solid 1.9.17: changing mode makes the new component, changing stock only changes the number without making the component again.
⚠️ Wrong imports and a heading that vanishes
- Importing from the wrong place.
PortalandDynamiclive in"solid-js/web"."solid-js"has no export by those names, so the import fails. component={tag}without parentheses. That sends the signal function itself, and Dynamic takes it for a component. Tested in Solid 1.9.17: only the texth1shows, the heading is gone. Writecomponent={tag()}.- Expecting the parent's CSS to reach the Portal. In the DOM, the Portal's content sits in
body, so selectors that rely on.cardmiss it. - The exercise's toggle. The grader clicks the first
<button>once and twice. The textDynamic Headingmust be the same inh1andh2.
TL;DR: <Portal> from "solid-js/web" puts its content in document.body (or in an element of your choice through mount={...}), out of the parent box that clips it, yet it stays part of the component tree: the parent component's onClick, context, and the Show around it still apply. <Dynamic component={tag()}> renders a tag or component picked while running, passes the other props on, and only makes its content again when the component changes. The exercise: Dynamic Heading becomes h2 after one click and is back to h1 after two.
⚡ Portal: render somewhere else
Write:
import { Portal } from "solid-js/web"
<Show when={open()}>
<Portal>
<div class="modal">...</div>
</Portal>
</Show>
Meaning: The content of Portal goes into document.body, so a card with overflow: hidden no longer clips the modal. mount={...} picks another place. Since in the DOM the modal is no longer a child of .card, CSS like .card .modal doesn't apply to it.
⚡ Events still follow the component tree
Guess first: the <div class="card"> around the Portal has an onClick. You click the text inside the modal. Does the card's onClick run?
It does, and card got the click prints. In the DOM the modal is a child of body, but in your code it's a child of BookCard, and Solid follows the second.
Meaning: Solid's onClick is delegated, so its path jumps from the Portal to the place <Portal> is written. Context gets through into the Portal too.
⚡ Dynamic: a tag picked while running
Write:
<Dynamic component={tag()}>Dynamic Heading</Dynamic>
<button onClick={() => setTag(t => t === "h1" ? "h2" : "h1")}>
Toggle Tag
</button>
Meaning: component takes a tag name as a string, or a component. The text stays, only the kind of element changes. Use the parentheses: component={tag} sends the function, and the heading is gone.