|
| 1 | +/** |
| 2 | + * Alpha (transparency) slider. |
| 3 | + * |
| 4 | + * Forked from |
| 5 | + * {@link https://github.com/casesandberg/react-color/blob/v2.19.3/src/components/common/Alpha.js | react-color's Alpha} |
| 6 | + * (MIT, Copyright (c) 2015 Case Sandberg). See the plugin LICENSE. |
| 7 | + * |
| 8 | + * @remarks |
| 9 | + * Kept as a class component for this round (function-component conversion is a |
| 10 | + * follow-up PR). `reactcss` has been removed in favor of plain inline styles, |
| 11 | + * and the unused custom `pointer` slot dropped. |
| 12 | + */ |
| 13 | +import {Component} from 'react' |
| 14 | +import type {CSSProperties, ReactElement} from 'react' |
| 15 | + |
| 16 | +import {Checkboard} from './Checkboard' |
| 17 | +import * as alpha from './helpers/alpha' |
| 18 | +import type { |
| 19 | + AlphaColorResult, |
| 20 | + CheckboardRenderers, |
| 21 | + ColorChangeHandler, |
| 22 | + HSLColor, |
| 23 | + PickerEvent, |
| 24 | + RGBColor, |
| 25 | +} from './types' |
| 26 | + |
| 27 | +export interface AlphaProps { |
| 28 | + rgb: RGBColor |
| 29 | + hsl: HSLColor |
| 30 | + a?: number | undefined |
| 31 | + direction?: 'horizontal' | 'vertical' | undefined |
| 32 | + radius?: string | undefined |
| 33 | + shadow?: string | undefined |
| 34 | + renderers?: CheckboardRenderers | undefined |
| 35 | + onChange?: ColorChangeHandler<AlphaColorResult> | undefined |
| 36 | +} |
| 37 | + |
| 38 | +export class Alpha extends Component<AlphaProps> { |
| 39 | + private container: HTMLDivElement | null = null |
| 40 | + |
| 41 | + override componentWillUnmount(): void { |
| 42 | + this.unbindEventListeners() |
| 43 | + } |
| 44 | + |
| 45 | + private readonly setContainerRef = (node: HTMLDivElement | null): void => { |
| 46 | + this.container = node |
| 47 | + } |
| 48 | + |
| 49 | + private readonly handleChange = (event: PickerEvent): void => { |
| 50 | + if (!this.container) { |
| 51 | + return |
| 52 | + } |
| 53 | + const change = alpha.calculateChange( |
| 54 | + event, |
| 55 | + this.props.hsl, |
| 56 | + this.props.direction, |
| 57 | + this.props.a, |
| 58 | + this.container, |
| 59 | + ) |
| 60 | + if (change && typeof this.props.onChange === 'function') { |
| 61 | + this.props.onChange(change) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private readonly handleMouseDown = (event: React.MouseEvent<HTMLDivElement>): void => { |
| 66 | + this.handleChange(event.nativeEvent) |
| 67 | + window.addEventListener('mousemove', this.handleChange) |
| 68 | + window.addEventListener('mouseup', this.handleMouseUp) |
| 69 | + } |
| 70 | + |
| 71 | + private readonly handleMouseUp = (): void => { |
| 72 | + this.unbindEventListeners() |
| 73 | + } |
| 74 | + |
| 75 | + private readonly unbindEventListeners = (): void => { |
| 76 | + window.removeEventListener('mousemove', this.handleChange) |
| 77 | + window.removeEventListener('mouseup', this.handleMouseUp) |
| 78 | + } |
| 79 | + |
| 80 | + override render(): ReactElement { |
| 81 | + const {rgb, direction, radius, shadow, renderers} = this.props |
| 82 | + const alphaValue = rgb.a ?? 1 |
| 83 | + const gradient = |
| 84 | + direction === 'vertical' |
| 85 | + ? `linear-gradient(to bottom, rgba(${rgb.r},${rgb.g},${rgb.b}, 0) 0%, rgba(${rgb.r},${rgb.g},${rgb.b}, 1) 100%)` |
| 86 | + : `linear-gradient(to right, rgba(${rgb.r},${rgb.g},${rgb.b}, 0) 0%, rgba(${rgb.r},${rgb.g},${rgb.b}, 1) 100%)` |
| 87 | + const pointerStyle: CSSProperties = |
| 88 | + direction === 'vertical' |
| 89 | + ? {position: 'absolute', left: 0, top: `${alphaValue * 100}%`} |
| 90 | + : {position: 'absolute', left: `${alphaValue * 100}%`} |
| 91 | + |
| 92 | + return ( |
| 93 | + <div style={{position: 'absolute', inset: 0, borderRadius: radius}}> |
| 94 | + <div style={{position: 'absolute', inset: 0, overflow: 'hidden', borderRadius: radius}}> |
| 95 | + <Checkboard renderers={renderers} /> |
| 96 | + </div> |
| 97 | + <div |
| 98 | + style={{ |
| 99 | + position: 'absolute', |
| 100 | + inset: 0, |
| 101 | + background: gradient, |
| 102 | + boxShadow: shadow, |
| 103 | + borderRadius: radius, |
| 104 | + }} |
| 105 | + /> |
| 106 | + {/* oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- the slider surface is dragged via pointer coordinates, which have no keyboard equivalent */} |
| 107 | + <div |
| 108 | + style={{position: 'relative', height: '100%', margin: '0 3px'}} |
| 109 | + ref={this.setContainerRef} |
| 110 | + onMouseDown={this.handleMouseDown} |
| 111 | + onTouchMove={this.handleChange} |
| 112 | + onTouchStart={this.handleChange} |
| 113 | + > |
| 114 | + <div style={pointerStyle}> |
| 115 | + <div |
| 116 | + style={{ |
| 117 | + width: '4px', |
| 118 | + borderRadius: '1px', |
| 119 | + height: '8px', |
| 120 | + boxShadow: '0 0 2px rgba(0, 0, 0, .6)', |
| 121 | + background: '#fff', |
| 122 | + marginTop: '1px', |
| 123 | + transform: 'translateX(-2px)', |
| 124 | + }} |
| 125 | + /> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + ) |
| 130 | + } |
| 131 | +} |
0 commit comments