High contrast
Introduction
Section titled “Introduction”The High contrast component provides a toggle for switching between normal and high contrast modes. It enhances focus indicators, link visibility, and overall visual clarity for users with low vision or those who prefer increased contrast. The component persists user choices and exposes a global JavaScript API for programmatic control.
When to use
Section titled “When to use”- Accessibility settings panels
- User preference controls
- Sites targeting users with low vision
- WCAG AAA compliance efforts
- Command palette / launcher integration
Quick example
Section titled “Quick example”Click the toggle below to switch between normal and high contrast mode:
Learn how to implement the HighContrast component in your project.
---import { HighContrast } from 'accessible-astro-components'---
<!-- Default high contrast toggle --><HighContrast />
<!-- With custom icons --><HighContrast> <Icon name="ion:contrast-outline" slot="off" /> <Icon name="ion:contrast" slot="on" /></HighContrast>
<!-- With custom label --><HighContrast label="Toggle high contrast mode" />Configure the HighContrast component using these available props to customize its behavior and appearance.
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | 'Toggle High Contrast' | Accessible label for the toggle button |
class | string | '' | Additional CSS classes to apply |
The HighContrast component supports named slots for customizing the icons displayed in normal and high contrast modes.
| Slot | Description |
|---|---|
off | Icon or content to display when high contrast is off |
on | Icon or content to display when high contrast is on |
JavaScript API
Section titled “JavaScript API”The HighContrast component exposes a global window.highContrast API for programmatic control. This is useful for integrating with command palettes, launchers, or other UI elements.
// Toggle high contrast modewindow.highContrast.toggle()
// Enable high contrast modewindow.highContrast.enable()
// Disable high contrast modewindow.highContrast.disable()
// Check if high contrast mode is enabledwindow.highContrast.isEnabled() // returns booleanEvents
Section titled “Events”The component dispatches a highcontrast:change custom event when the mode changes:
document.addEventListener('highcontrast:change', (e) => { console.log('High contrast enabled:', e.detail.enabled)})Launcher integration
Section titled “Launcher integration”The component automatically listens for launcher:action events with action: 'toggle-high-contrast', making it compatible with the Accessible Astro Launcher out of the box.
Accessibility
Section titled “Accessibility”Accessibility isn’t an afterthought - it’s built into the core of this component. The HighContrast component is built with accessibility in mind:
- Uses semantic button element
- Proper ARIA attributes (
aria-pressed,aria-label) - Maintains focus states
- Persists across page loads using
localStorage - Enhances focus indicators site-wide when active
- Improves link visibility with underlines
Styling
Section titled “Styling”Make the HighContrast component your own with custom styling while maintaining its accessibility features.
Default high contrast styles
Section titled “Default high contrast styles”The component includes global styles that are applied when high contrast mode is active:
/* These styles are automatically applied */.high-contrast *:focus-visible { outline-width: 3px; outline-style: solid;}
.high-contrast a:not([class]) { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 2px;}Extending high contrast styles
Section titled “Extending high contrast styles”You can add your own high contrast styles by targeting the .high-contrast class:
<style is:global> .high-contrast { /* Increase text contrast */ --text-color: hsl(0 0% 0%); --bg-color: hsl(0 0% 100%); }
.high-contrast button, .high-contrast a { /* Thicker borders */ border-width: 2px; }
.high-contrast img { /* Add borders to images */ outline: 2px solid currentColor; }</style><HighContrast class="text-lg border-2 hover:border-4"> <Icon name="ion:contrast-outline" slot="off" class="w-6 h-6" /> <Icon name="ion:contrast" slot="on" class="w-6 h-6" /></HighContrast>Interactive examples
Section titled “Interactive examples”See the HighContrast component in action with these practical examples.