shadcn.io is not affiliated with official shadcn/ui
Cookie Consent Dialog Block for React
A recipe blog's first-visit cookie card shown open over its page: two sentences, then Reject all and Accept all as two equal outline buttons side by side, with no primary. It isn't modal, so the page stays readable, and the choice is written on the page's foot.
Installation
Overview
Ask once, plainly, and let people read on, with this cookie card for React with Base UI. It sits on an article page of Windmill Notes, a recipe-notes blog: the header holds an outline Cookie choices Button, and the page itself is drawn inside a hairline frame with a plus mark on each corner, a cover, a title and two columns of text over a foot that says what was chosen. The card says in two sentences what the optional cookies do and that none is on until you choose, then offers Reject all and Accept all as two equal outline buttons, side by side, the same size and weight, with Reject first. There is no Shine Button on purpose: neither answer is the one the design wants. Choosing hides the card and writes the page's foot ("Cookies: rejected, analytics and marketing off"). It isn't modal: there is no scrim, nothing is trapped and the page keeps working. For preview the card is drawn open in place at the foot of the page when it loads; the Cookie choices button opens the real card as a non-modal Popover, with the same two answers. For blogs, news sites, shops and any page that has to ask before it measures.
FAQ
Why is the card already open, and what do I keep?
So you can see it without a click. On load the card is a copy drawn in place at the foot of the page: a native dialog element that isn't modal, with no scrim, so nothing is trapped, hidden or focused, and the page around it keeps working. The Cookie choices button in the header opens the real card as a Popover, and the copy stays closed until a reload. Both show the same ConsentButtons component and share the block's state. In your app keep a card of your own fixed to the foot of the page, non-modal, with these two buttons, and drop the copy: the "in-place" state and the dialog element inside the frame.
Why are the two buttons the same?
Because a consent that is easier to give than to refuse isn't a free choice. Both are outline buttons of the same size, side by side, with Reject all first, and the card has nothing to scroll past. If your law or your policy asks for a way to set categories one by one, add a third control that opens a preference centre: that is the Cookie Settings dialog in this category. Link your cookie policy from the last sentence; the demo has no policy page to link.
What does choosing do, and how do I wire it up?
Here it sets the choice in local state, hides the card, writes it on the drawn page's foot and says in a polite status message that nothing was stored. No cookie is set. In your app write the choice to your consent store in choose, set or clear the optional cookies and scripts from it, and show the card again whenever there is no stored choice or the policy changes.
Why is the real card a Popover?
Because a modal would trap focus and hide the page, which is what this card promises not to do, and a Popover is the one overlay in all four libraries that isn't modal. It is a Popover, which is not modal in this library, anchored to the button. Closing it without a choice leaves the choice unmade. In your app the card is a fixed non-modal panel at the foot of the page, which is what the copy shown on load draws.
Is it accessible from the keyboard?
Yes. The real card is named by its title and described by its two sentences, focus moves into it when it opens (onto Reject all), and Escape closes it and returns focus to the Cookie choices button; Tab can leave it, since it isn't modal. Both answers are real buttons with their names. The copy shown on load moves no focus; Escape inside it closes it and puts focus on the button, and choosing either answer does the same.
Related
- Pro
Cookie Consent Footer
PreferencesA footer for a writing app whose legal row opens a cookie settings dialog with a switch per category and shows what was saved.
- Pro
Cookie Settings Dialog
Confirm and consentA newspaper's cookie preference centre shown open over its front page: four categories with switches and counts, Essential locked on, and for the chosen one a sentence and a table of its actual cookies, then Reject non-essential, Accept all and Save choices.
- Pro
The cookies on a person's own browser as a table of eleven typed cookies with name, provider, purpose and dates, four category switches that clear what they cover, a search and a delete that asks first.
- Pro
Cookie Consent Login
Sign-up and consentA bakery subscription's cookie choices shown open over a drawn sign-in page: Essential locked on, Analytics and Marketing switches that start off, and three equal buttons, Reject all, Accept all and Save choices.
- Pro
Cookie Consent Multi-Action Banner
Choices and promptsA cookie prompt for an online shop: one split card with a stack of three equal-width choices, Accept all, Reject non-essential and Manage preferences.
- Pro
App Rating Dialog
Drawers and popoversA water tracker's Today screen with its rating drawer shown open over a drawn phone: five stars with a word each, then a branch, where four or five stars offers the store and one to three asks what could be better, with topics and a note.