$npx -y skills add dylantarre/design-system-skills --skill focus-statesGenerates accessible focus indicators meeting WCAG 2.4.7 and 2.4.11 requirements. Use when styling :focus-visible, keyboard navigation indicators, or fixing focus ring visibility issues.
| 1 | # Focus States Generator |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Generate accessible, visible focus indicators for interactive elements. Creates focus styles that meet WCAG 2.4.7 (Focus Visible) and 2.4.11 (Focus Appearance) requirements while maintaining visual design consistency. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Setting up focus styles for a design system |
| 10 | - Ensuring keyboard navigation is visible |
| 11 | - Creating custom focus rings that match brand |
| 12 | - Fixing "invisible" focus states in existing UI |
| 13 | |
| 14 | ## Quick Reference: WCAG Requirements |
| 15 | |
| 16 | | Criterion | Requirement | Level | |
| 17 | |-----------|-------------|-------| |
| 18 | | 2.4.7 Focus Visible | Focus indicator must be visible | AA | |
| 19 | | 2.4.11 Focus Appearance | 2px+ perimeter, 3:1 contrast | AAA | |
| 20 | | 2.4.12 Focus Not Obscured | Focus not hidden by other content | AA | |
| 21 | |
| 22 | ## The Process |
| 23 | |
| 24 | 1. **Get brand colors**: Primary color and background colors |
| 25 | 2. **Ask style preference**: |
| 26 | - Ring (outline around element) |
| 27 | - Glow (soft shadow-based) |
| 28 | - Underline (for text links) |
| 29 | - Hybrid (ring + offset) |
| 30 | 3. **Ask visibility approach**: Always visible or focus-visible only? |
| 31 | 4. **Generate**: Create focus tokens and utility classes |
| 32 | 5. **Test guidance**: Provide keyboard testing checklist |
| 33 | |
| 34 | ## Focus Style Options |
| 35 | |
| 36 | | Style | Character | Best For | |
| 37 | |-------|-----------|----------| |
| 38 | | Ring | Clean, defined | Buttons, inputs, cards | |
| 39 | | Glow | Soft, modern | Dark themes, premium UI | |
| 40 | | Underline | Minimal | Text links, nav items | |
| 41 | | Offset | High contrast | When ring blends with element | |
| 42 | | Inset | Subtle | Contained elements | |
| 43 | |
| 44 | ## Output Formats |
| 45 | |
| 46 | **CSS Custom Properties + Utilities:** |
| 47 | ```css |
| 48 | :root { |
| 49 | /* Focus tokens */ |
| 50 | --focus-ring-color: #2563eb; |
| 51 | --focus-ring-width: 2px; |
| 52 | --focus-ring-offset: 2px; |
| 53 | --focus-ring-style: solid; |
| 54 | |
| 55 | /* Computed focus ring */ |
| 56 | --focus-ring: var(--focus-ring-width) var(--focus-ring-style) var(--focus-ring-color); |
| 57 | --focus-ring-offset-shadow: 0 0 0 var(--focus-ring-offset) var(--color-background); |
| 58 | --focus-ring-shadow: 0 0 0 calc(var(--focus-ring-offset) + var(--focus-ring-width)) var(--focus-ring-color); |
| 59 | } |
| 60 | |
| 61 | /* Base focus reset */ |
| 62 | *:focus { |
| 63 | outline: none; |
| 64 | } |
| 65 | |
| 66 | /* Focus-visible for keyboard only */ |
| 67 | *:focus-visible { |
| 68 | outline: var(--focus-ring); |
| 69 | outline-offset: var(--focus-ring-offset); |
| 70 | } |
| 71 | |
| 72 | /* Alternative: box-shadow approach (more styling control) */ |
| 73 | .focus-ring:focus-visible { |
| 74 | outline: none; |
| 75 | box-shadow: |
| 76 | var(--focus-ring-offset-shadow), |
| 77 | var(--focus-ring-shadow); |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | **Component-Specific Focus:** |
| 82 | ```css |
| 83 | /* Buttons */ |
| 84 | .btn:focus-visible { |
| 85 | outline: 2px solid var(--focus-ring-color); |
| 86 | outline-offset: 2px; |
| 87 | } |
| 88 | |
| 89 | /* Inputs - inset focus */ |
| 90 | .input:focus-visible { |
| 91 | outline: none; |
| 92 | border-color: var(--focus-ring-color); |
| 93 | box-shadow: 0 0 0 3px rgb(37 99 235 / 0.2); |
| 94 | } |
| 95 | |
| 96 | /* Cards - offset ring */ |
| 97 | .card:focus-visible { |
| 98 | outline: 2px solid var(--focus-ring-color); |
| 99 | outline-offset: 4px; |
| 100 | } |
| 101 | |
| 102 | /* Links - underline enhancement */ |
| 103 | a:focus-visible { |
| 104 | outline: none; |
| 105 | text-decoration-thickness: 2px; |
| 106 | text-underline-offset: 4px; |
| 107 | background-color: rgb(37 99 235 / 0.1); |
| 108 | border-radius: 2px; |
| 109 | } |
| 110 | |
| 111 | /* Skip link */ |
| 112 | .skip-link:focus { |
| 113 | position: fixed; |
| 114 | top: 1rem; |
| 115 | left: 1rem; |
| 116 | z-index: 9999; |
| 117 | padding: 1rem; |
| 118 | background: var(--color-background); |
| 119 | outline: 2px solid var(--focus-ring-color); |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | **Tailwind Config:** |
| 124 | ```js |
| 125 | module.exports = { |
| 126 | theme: { |
| 127 | extend: { |
| 128 | ringColor: { |
| 129 | DEFAULT: '#2563eb', |
| 130 | }, |
| 131 | ringWidth: { |
| 132 | DEFAULT: '2px', |
| 133 | }, |
| 134 | ringOffsetWidth: { |
| 135 | DEFAULT: '2px', |
| 136 | }, |
| 137 | } |
| 138 | }, |
| 139 | plugins: [ |
| 140 | // Custom focus-visible variant |
| 141 | plugin(function({ addVariant }) { |
| 142 | addVariant('focus-visible-within', '&:has(:focus-visible)') |
| 143 | }) |
| 144 | ] |
| 145 | } |
| 146 | ``` |
| 147 | |
| 148 | **Tailwind Utilities:** |
| 149 | ```html |
| 150 | <!-- Standard focus ring --> |
| 151 | <button class="focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 focus-visible:ring-offset-2"> |
| 152 | Button |
| 153 | </button> |
| 154 | |
| 155 | <!-- Glow style --> |
| 156 | <button class="focus-visible:outline-none focus-visible:ring-4 focus-visible:ring-blue-500/30"> |
| 157 | Glow Button |
| 158 | </button> |
| 159 | ``` |
| 160 | |
| 161 | **JSON Tokens:** |
| 162 | ```json |
| 163 | { |
| 164 | "focus": { |
| 165 | "ring": { |
| 166 | "color": { "value": "{color.primary.500}" }, |
| 167 | "width": { "value": "2px" }, |
| 168 | "offset": { "value": "2px" }, |
| 169 | "style": { "value": "solid" } |
| 170 | }, |
| 171 | "glow": { |
| 172 | "color": { "value": "{color.primary.500}" }, |
| 173 | "blur": { "value": "4px" }, |
| 174 | "spread": { "value": "2px" }, |
| 175 | "opacity": { "value": "0.3" } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | ``` |
| 180 | |
| 181 | ## Dark Mode Focus |
| 182 | |
| 183 | ```css |
| 184 | /* Light mode */ |
| 185 | :root { |
| 186 | --focus-ring-color: #2563eb; |
| 187 | --focus-ring-offset-color: #ffffff; |
| 188 | } |
| 189 | |
| 190 | /* Dark mode - increase visibility */ |
| 191 | :root.dark { |
| 192 | --focus-ring-color: #60a5fa; |
| 193 | --focus-ring-offset-color: #1f2937; |
| 194 | } |
| 195 | ``` |
| 196 | |
| 197 | ## Focus Indicator Contrast |
| 198 | |
| 199 | WCAG 2.4.11 requires 3:1 contrast between: |
| 200 | 1. Focus indi |