|
1 | | -## Description |
| 1 | +## Overview |
2 | 2 |
|
3 | | -The `<sp-color-handle>` is used to select a colour on an `<sp-color-area>`, `<sp-color-slider>`, or `<sp-color-wheel>`. It functions similarly to the handle on an `<sp-slider>`. |
| 3 | +The `<sp-color-handle>` is used to select a color on an `<sp-color-area>`, `<sp-color-slider>`, or `<sp-color-wheel>`. It provides a draggable control point for precise color selection within color components. |
4 | 4 |
|
5 | 5 | ### Usage |
6 | 6 |
|
7 | 7 | [](https://www.npmjs.com/package/@spectrum-web-components/color-handle) |
8 | 8 | [](https://bundlephobia.com/result?p=@spectrum-web-components/color-handle) |
9 | 9 |
|
10 | | -``` |
| 10 | +**Note**: `<sp-color-handle>` is a primitive component designed to be used within other color selection components. It's not typically used directly in applications, but rather as part of higher-level color components like `<sp-color-area>`, `<sp-color-slider>`, or `<sp-color-wheel>`. |
| 11 | + |
| 12 | +```bash |
11 | 13 | yarn add @spectrum-web-components/color-handle |
12 | 14 | ``` |
13 | 15 |
|
14 | 16 | Import the side effectful registration of `<sp-color-handle>` via: |
15 | 17 |
|
16 | | -``` |
| 18 | +```javascript |
17 | 19 | import '@spectrum-web-components/color-handle/sp-color-handle.js'; |
18 | 20 | ``` |
19 | 21 |
|
20 | 22 | When looking to leverage the `ColorHandle` base class as a type and/or for extension purposes, do so via: |
21 | 23 |
|
22 | | -``` |
| 24 | +```javascript |
23 | 25 | import { ColorHandle } from '@spectrum-web-components/color-handle'; |
24 | 26 | ``` |
25 | 27 |
|
26 | | -## Standard |
| 28 | +### Anatomy |
| 29 | + |
| 30 | +The color handle consists of several key parts: |
| 31 | + |
| 32 | +- A visual handle element that indicates the current position |
| 33 | +- Touch-responsive interaction areas |
| 34 | +- Color display showing the current selected color |
| 35 | +- Opacity checkerboard pattern for transparent colors |
| 36 | +- An optional `sp-color-loupe` that appears above the handle when the properties `open = true` and `disabled = false` |
27 | 37 |
|
28 | 38 | ```html |
29 | 39 | <sp-color-handle></sp-color-handle> |
30 | 40 | ``` |
31 | 41 |
|
32 | | -## Disabled |
| 42 | +### Options |
| 43 | + |
| 44 | +#### Color |
| 45 | + |
| 46 | +The `color` property sets the visual color displayed within the handle. This accepts any valid CSS color format. The default color is `rgba(255, 0, 0, 0.5)` (semi-transparent red). |
| 47 | + |
| 48 | +For a complete list of supported color formats, see the [ColorController documentation](/tools/color-controller#supported-color-formats). |
| 49 | + |
| 50 | +**Transparency Support**: When using transparent colors, the handle displays an opacity checkerboard pattern background to clearly show the transparency level. |
| 51 | + |
| 52 | +```html |
| 53 | +<div style="display: flex; gap: 16px; align-items: center; margin: 16px 0;"> |
| 54 | + <!-- Hex color --> |
| 55 | + <div style="position: relative; height: 20px; margin: 20px;"> |
| 56 | + <sp-color-handle color="#ff0000"></sp-color-handle> |
| 57 | + </div> |
| 58 | + |
| 59 | + <!-- RGB format --> |
| 60 | + <div style="position: relative; height: 20px; margin: 20px;"> |
| 61 | + <sp-color-handle color="rgb(255, 0, 0)"></sp-color-handle> |
| 62 | + </div> |
| 63 | + |
| 64 | + <!-- RGBA format with transparency --> |
| 65 | + <div style="position: relative; height: 20px; margin: 20px;"> |
| 66 | + <sp-color-handle color="rgba(255, 0, 0, 0.5)"></sp-color-handle> |
| 67 | + </div> |
| 68 | + |
| 69 | + <!-- HSL format --> |
| 70 | + <div style="position: relative; height: 20px; margin: 20px;"> |
| 71 | + <sp-color-handle color="hsl(0, 100%, 50%)"></sp-color-handle> |
| 72 | + </div> |
| 73 | + |
| 74 | + <!-- Named colors --> |
| 75 | + <div style="position: relative; height: 20px; margin: 20px;"> |
| 76 | + <sp-color-handle color="red"></sp-color-handle> |
| 77 | + </div> |
| 78 | +</div> |
| 79 | +``` |
| 80 | + |
| 81 | +### States |
| 82 | + |
| 83 | +#### Standard |
| 84 | + |
| 85 | +The default state of the color handle, ready for interaction: |
| 86 | + |
| 87 | +```html |
| 88 | +<sp-color-handle></sp-color-handle> |
| 89 | +``` |
| 90 | + |
| 91 | +#### Disabled |
| 92 | + |
| 93 | +A disabled color handle shows that the control exists but is not available for interaction. This maintains layout continuity and communicates that the handle may become available later: |
33 | 94 |
|
34 | 95 | ```html |
35 | 96 | <sp-color-handle disabled></sp-color-handle> |
36 | 97 | ``` |
37 | 98 |
|
38 | | -## Open |
| 99 | +#### Open |
39 | 100 |
|
40 | | -When the `<sp-color-handle>` uses the `open` property, the `<sp-color-loupe>` component can be used above the handle to show the selected color that would otherwise be covered by a mouse, stylus, or finger on the down/touch state. This can be customized to appear only on finger-input, or always appear regardless of input type. |
| 101 | +When the `open` property is set, the `<sp-color-loupe>` component appears above the handle to show the selected color that would otherwise be covered by a mouse, stylus, or finger on the down/touch state. The loupe automatically appears for touch input (`pointerType === 'touch'`). |
41 | 102 |
|
42 | 103 | ```html |
43 | 104 | <div style="height: 72px"></div> |
44 | 105 | <sp-color-handle open></sp-color-handle> |
45 | 106 | ``` |
| 107 | + |
| 108 | +**Automatic Behavior**: The loupe automatically opens when touched and closes when the touch interaction ends. For mouse and stylus input, the loupe remains hidden by default unless explicitly set to `open="true"`. |
| 109 | + |
| 110 | +#### Focused |
| 111 | + |
| 112 | +The color handle can receive keyboard focus when used within interactive color components. The focused state is managed automatically by the parent component and is indicated visually: |
| 113 | + |
| 114 | +```html |
| 115 | +<sp-color-handle focused></sp-color-handle> |
| 116 | +``` |
| 117 | + |
| 118 | +### Behaviors |
| 119 | + |
| 120 | +#### Pointer Interactions |
| 121 | + |
| 122 | +The color handle automatically manages pointer events to provide the optimal user experience: |
| 123 | + |
| 124 | +- **Touch Input**: When touched (`pointerType === 'touch'`), the color loupe automatically appears to prevent the finger from obscuring the selected color |
| 125 | +- **Mouse/Stylus Input**: The loupe remains hidden by default for precision pointing devices |
| 126 | +- **Pointer Capture**: The handle captures pointer events during interaction to ensure smooth dragging even when the pointer moves outside the handle area |
| 127 | +- **Event Handling**: The component listens for `pointerdown`, `pointerup`, and `pointercancel` events to manage the interaction lifecycle |
| 128 | + |
| 129 | +#### Color Display |
| 130 | + |
| 131 | +The handle displays the current color with proper support for transparency: |
| 132 | + |
| 133 | +- Transparent colors are shown with an opacity checkerboard pattern background |
| 134 | +- The color updates in real-time as the user interacts with the parent color component |
| 135 | +- Supports all standard CSS color formats |
| 136 | + |
| 137 | +For a complete list of supported color formats, see the [ColorController documentation](/tools/color-controller#supported-color-formats). |
| 138 | + |
| 139 | +### Accessibility |
| 140 | + |
| 141 | +The `<sp-color-handle>` is designed to work as part of accessible color selection components: |
| 142 | + |
| 143 | +#### Keyboard Support |
| 144 | + |
| 145 | +While the color handle itself is not directly keyboard accessible, it works in conjunction with its parent components ([`<sp-color-area>`](/components/color-area), [`<sp-color-slider>`](/components/color-slider), [`<sp-color-wheel>`](/components/color-wheel)) which provide comprehensive keyboard navigation. |
| 146 | +Example: Keyboard accessibility with `sp-color-area` as parent component |
| 147 | + |
| 148 | +```html |
| 149 | +<sp-color-area></sp-color-area> |
| 150 | +``` |
| 151 | + |
| 152 | +#### Screen Reader Support |
| 153 | + |
| 154 | +The color handle is rendered as a visual indicator and does not directly interface with screen readers. Accessibility is provided through the parent color component's ARIA implementation. |
| 155 | + |
| 156 | +#### Touch Accessibility |
| 157 | + |
| 158 | +- **Color Loupe**: Automatically appears for touch input to ensure the selected color remains visible |
| 159 | +- **Large Touch Target**: The handle provides an appropriately sized touch target for mobile interaction |
| 160 | +- **Pointer Capture**: Ensures reliable dragging behavior across different touch devices |
| 161 | + |
| 162 | +#### Focus Management |
| 163 | + |
| 164 | +Focus is managed by the parent color component, with the handle reflecting the focused state visually when its parent component has keyboard focus. |
0 commit comments