Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Create Poll Dialog Block for React

A neighbourhood forum thread with a poll builder shown open: a question, two to six options, one answer or several, how long it stays open, and a live preview of the poll as voters will see it.

Framework
UI library

Preview

Preview width

Installation

Part of Pro
Pro unlocks the code and one-line install of every Pro button and text effect, plus the MCP for your AI editor.
Have Pro already? Sign in to open it.Opens here as soon as you're Pro.

Overview

Let people ask the street a question without leaving the thread, with this poll builder for React with Base UI. It opens from a Street party planning thread on Benchtalk, a neighbourhood forum: the page's header holds an outline Create poll Button, and the thread, a question post, two replies and a composer, is drawn under a soft scrim inside a hairline frame with a plus mark on each corner. The Dialog opens with a question Input of up to 120 characters, a Switch for more than one answer and a Select for how long the poll stays open. Under them it splits: at the start two to six option inputs (Add option, Remove, never fewer than two, each named like Option 3 of 4), at the end the poll exactly as voters will see it, a circle by each option for one answer or a square for several, and "Closes Mon 12 Oct", worked out from a fixed date. Publish poll, the only Shine Button, waits for a question and two different options and says what is missing; publishing puts the poll card in the drawn thread. For preview the dialog is drawn open in place when the page loads; the header button opens the real Dialog, with the same form and the same state. For forums, community boards, event planning and any thread that needs a quick answer.

FAQ

Why is the dialog already open, and what do I keep?

So you can see it without a click. On load the dialog is a copy drawn in place over the page: a native dialog element that isn't modal, so nothing is trapped, hidden or focused, and the page around the preview keeps working. The Create poll button opens the real Dialog, modal, with its focus trap, Escape and focus return, and the copy stays closed until a reload. Both show the same PollFields component and share the block's state, so a question typed in the copy is there in the real one. In your app keep the Dialog and drop the copy: the "in-place" state, the scrim and the dialog element inside the frame.

How do the options work?

Each option is an input named for its place ("Option 3 of 4"). Add option adds one and moves focus into it; Remove takes one out and moves focus to the neighbour. The most is six and the fewest is two: at the limit the button stays in place and says why in a polite message ("A poll needs at least two options.", "6 options is the most a poll takes.") instead of disappearing. The list is a fixed height and scrolls inside itself, so adding a fifth or a sixth option never moves the dialog.

How does the preview stay true to the poll?

It is drawn from the same state as the form, on every keystroke: the question, the options that are filled in, a circle for one answer or a square once the switch is on, "Choose one." or "Choose any.", and the closing day. The closing day is the fixed AS_OF plus the length you picked (1 day, 3 days or 1 week), so with the preview's date it reads Mon 12 Oct for three days. The preview list is also a fixed height, and it is a tab stop so a keyboard can scroll it. On a phone the preview folds into a Preview the poll button under the form.

What does Publish poll wait for?

A question, and at least two options that are filled in and different from each other. Until then the button is quiet but still answers a press: the reason is on the line under the form ("Add a question to publish.", "Add one more option to publish.", "Two options are the same."), pressing says it in a polite message and puts focus on the field that needs it. The question stops at 120 characters, with a visible count and a spoken one only at 20, 10 and 0 left.

Does it publish anything, and how do I wire it up?

No. Publish poll puts the poll card in the drawn thread, closes the dialog and says "Nothing was posted in this demo" in a polite message. The dialog goes back to its opening state each time it closes. In your app post { question, choices, multiple, closes } from publish, let the server pick the closing time, and keep the same preview. The options and the question are typed here as examples.

Is it accessible from the keyboard?

Yes. The real dialog is named by its title and described by its sentence, focus starts in the question, and Escape, Cancel and the close button all return focus to the header button. The trigger is passed to DialogTrigger through render, and the dialog is controlled with open and onOpenChange. The switch is named for what it does, the length is a select named Open for, the preview option list is labelled, and one polite status message reports every change. The copy shown on load moves no focus; Escape inside it, Cancel and its close button close it and put focus on the header button.

  • Pro

    Quick Poll Dialog

    Notes and editing

    A team meeting agenda with a one-question poll shown open: four large rows to vote on, then the results in the same space with bars, counts in words and shares that add to 100, and a way to change the vote.

  • Pro

    Feature Request Dialog

    Feedback and reports

    A public roadmap with a request dialog shown open: the title finds similar requests as you type, each with a vote toggle and its count, voting instead closes the dialog, and a new request lands on top of the board's Considering lane.

  • Pro

    Merge Accounts Dialog

    Notes and editing

    A contacts page with its merge dialog shown open: two accounts that look like one person in a table, matching fields marked Same, a radio in each cell where they differ, a Result column that follows your choices, and Merge accounts waiting until every conflict is chosen.

  • Pro

    Quick Note Dialog

    Notes and editing

    A sticky-note wall with its dialog shown open: a title, a note of up to 280 characters, an Idea, To-do, Reference or Later label, a reminder day on a calendar and a pin switch, and a new card that lands first on the wall.

  • Pro

    Reorder Items Dialog

    Notes and editing

    A landing-page builder's reorder dialog shown open: five page sections with grips you lift with Space and move with the arrow keys, Move up and down buttons, a spoken position, and the page behind it rearranging on save.

  • Pro

    Save Draft Dialog

    Notes and editing

    A publishing editor with its save-draft dialog shown open: a name, a folder, an autosave choice with a sentence that follows it, and a discard that asks again in the same row.