Modal focus trap

Write codeEasy15 minFigma and Microsoft style

Your task

A modal dialog traps keyboard focus, and a test harness reproduces that from data so the team can assert it without a browser. Each element inside the dialog arrives as one string: its id, a vertical bar, then its state, which is ready, disabled or hidden. The list is in document order and ids are unique. Only a ready element can hold focus. Focus starts on start_id when that element is ready, and otherwise on the dialog itself, written as an empty string. keys holds the presses in order, each either Tab or Shift+Tab. Tab moves to the next ready element, wrapping from the last round to the first. Shift+Tab moves to the previous, wrapping from the first round to the last. From the dialog itself, Tab lands on the first ready element and Shift+Tab on the last. Return the id focused after each press, in order. When nothing is ready, every press reports the empty string.

Examples

focus_trap_sequence(elements: list[str], start_id: str, keys: list[str]) -> list[str]
Example 1
Inputfocus_trap_sequence(["close|ready", "name|ready", "save|ready"], "name", ["Tab", "Tab", "Tab"])
Output["save", "close", "name"]
Example 2
Inputfocus_trap_sequence(["close|ready", "name|disabled", "save|ready"], "close", ["Tab", "Shift+Tab", "Shift+Tab"])
Output["save", "close", "save"]
Example 3
Inputfocus_trap_sequence(["close|ready", "name|ready"], "", ["Shift+Tab", "Tab"])
Output["name", "close"]