|
1 | | -## Description |
| 1 | +## Overview |
2 | 2 |
|
3 | | -The `opacity-checkerboard` class is used to highlight opacity. Leverage these styles in your component as outlined below to unify you application's visuals with those delivered by various Spectrum Web Components. |
| 3 | +The `opacity-checkerboard` class provides a CSS utility that displays a checkerboard pattern background, commonly used to highlight transparent or semi-transparent areas in UI components. This visual indicator helps users distinguish between transparent and opaque regions, making it an essential tool for color pickers, image editors, and other components that work with opacity. |
4 | 4 |
|
5 | | -## Usage |
| 5 | +### Usage |
6 | 6 |
|
7 | | -Import the styles from the `opacity-checkerboard` CSS: |
| 7 | +[](https://www.npmjs.com/package/@spectrum-web-components/opacity-checkerboard) |
| 8 | +[](https://bundlephobia.com/result?p=@spectrum-web-components/opacity-checkerboard) |
8 | 9 |
|
| 10 | +```bash |
| 11 | +yarn add @spectrum-web-components/opacity-checkerboard |
9 | 12 | ``` |
| 13 | + |
| 14 | +Import the opacity checkerboard styles from: |
| 15 | + |
| 16 | +```javascript |
10 | 17 | import opacityCheckerBoardStyles from '@spectrum-web-components/opacity-checkerboard/src/opacity-checkerboard.css.js'; |
11 | 18 | ``` |
12 | 19 |
|
13 | | -Add it to your component's styles array before your component's styles. The order that you include the styles in makes a difference, because selectors within opacity checkerboard may have the same |
14 | | -specificity as those within your component. |
| 20 | +### Integration |
15 | 21 |
|
16 | | -```js |
| 22 | +To integrate the opacity checkerboard styles into your component, add them to your component's styles array. The order of inclusion is important, as selectors within opacity checkerboard may have the same specificity as those within your component: |
| 23 | + |
| 24 | +```javascript |
17 | 25 | public static override get styles(): CSSResultArray { |
18 | 26 | return [opacityCheckerBoardStyles, styles]; |
19 | 27 | } |
20 | 28 | ``` |
21 | 29 |
|
22 | | -Use the `opacity-checkerboard` class in your component's `render()` method: |
| 30 | +### Examples |
| 31 | + |
| 32 | +#### Basic usage |
| 33 | + |
| 34 | +Apply the `opacity-checkerboard` class to any element that needs to display the checkerboard pattern: |
23 | 35 |
|
24 | 36 | ```html-live demo |
25 | 37 | <div |
26 | 38 | class="opacity-checkerboard" |
27 | 39 | style="inline-size: 100px; block-size: 100px;" |
| 40 | + aria-hidden="true" |
28 | 41 | ></div> |
29 | 42 | ``` |
| 43 | + |
| 44 | +#### With overlaid content |
| 45 | + |
| 46 | +The opacity checkerboard works well as a background for elements with partial transparency: |
| 47 | + |
| 48 | +```html-live |
| 49 | +<div |
| 50 | + class="opacity-checkerboard" |
| 51 | + style="inline-size: 200px; block-size: 200px; position: relative;" |
| 52 | + aria-hidden="true" |
| 53 | +> |
| 54 | + <div |
| 55 | + style=" |
| 56 | + position: absolute; |
| 57 | + inset: 0; |
| 58 | + background-color: rgba(255, 0, 0, 0.5); |
| 59 | + " |
| 60 | + ></div> |
| 61 | +</div> |
| 62 | +``` |
| 63 | + |
| 64 | +### Accessibility |
| 65 | + |
| 66 | +When implementing the opacity checkerboard pattern, ensure proper accessibility by: |
| 67 | + |
| 68 | +- **Hiding from screen readers**: Use `aria-hidden="true"` on the checkerboard element since it's purely visual |
| 69 | +- **Providing semantic information**: Use separate text nodes or live regions to convey opacity information |
| 70 | +- **Using live regions**: Implement `aria-live` regions to announce opacity changes dynamically |
| 71 | +- **Descriptive context**: Provide meaningful descriptions of the actual color and opacity values, not the visual pattern |
| 72 | + |
| 73 | +#### Screen reader support |
| 74 | + |
| 75 | +The opacity checkerboard is a purely visual indicator and should remain hidden from screen readers. Instead, provide semantic information through separate text nodes or live regions that update as opacity changes: |
| 76 | + |
| 77 | +```html-live |
| 78 | +<div class="color-container"> |
| 79 | + <div |
| 80 | + class="opacity-checkerboard" |
| 81 | + style="inline-size: 100px; block-size: 100px;" |
| 82 | + aria-hidden="true" |
| 83 | + ></div> |
| 84 | + <div class="visually-hidden" aria-live="polite" id="opacity-status"> |
| 85 | + Current opacity: 75% |
| 86 | + </div> |
| 87 | +</div> |
| 88 | +``` |
| 89 | + |
| 90 | +For components that change opacity dynamically, use a live region to announce changes: |
| 91 | + |
| 92 | +```html-live |
| 93 | +<div class="dynamic-opacity-example"> |
| 94 | + <div |
| 95 | + class="opacity-checkerboard" |
| 96 | + style="inline-size: 100px; block-size: 100px; position: relative;" |
| 97 | + aria-hidden="true" |
| 98 | + > |
| 99 | + <div |
| 100 | + style=" |
| 101 | + position: absolute; |
| 102 | + inset: 0; |
| 103 | + background-color: rgba(255, 0, 0, 0.6); |
| 104 | + " |
| 105 | + ></div> |
| 106 | + </div> |
| 107 | + <div aria-live="assertive" class="visually-hidden"> |
| 108 | + Opacity changed to 60% |
| 109 | + </div> |
| 110 | +</div> |
| 111 | +``` |
| 112 | + |
| 113 | +#### Keyboard navigation |
| 114 | + |
| 115 | +When the opacity checkerboard is part of an interactive component, ensure it doesn't interfere with keyboard navigation: |
| 116 | + |
| 117 | +```html-live |
| 118 | +<button |
| 119 | + class="color-button" |
| 120 | + aria-label="Select red color with 50% opacity" |
| 121 | + aria-describedby="opacity-description" |
| 122 | +> |
| 123 | + <span |
| 124 | + class="opacity-checkerboard" |
| 125 | + style="inline-size: 40px; block-size: 40px; display: block;" |
| 126 | + aria-hidden="true" |
| 127 | + ></span> |
| 128 | + <span id="opacity-description" class="visually-hidden"> |
| 129 | + Red color with 50% transparency |
| 130 | + </span> |
| 131 | +</button> |
| 132 | +``` |
0 commit comments