import BaseFoundation, { DefaultAdapter } from "../base/foundation"; import ColorPickerFoundation, { ColorPickerAdapter, ColorPickerProps, ColorPickerState } from "./foundation"; import { HsvaColor } from "./interface"; export interface AlphaSliderBaseProps { width: number; height: number; hsva: HsvaColor; handleSize: number; foundation: ColorPickerFoundation } export interface AlphaSliderBaseState { handlePosition: number; isHandleGrabbing: boolean } export interface AlphaSliderAdapter

, S = Record> extends DefaultAdapter { handleMouseDown: (e: any) => void; handleMouseUp: (e: any) => void; getColorPickerFoundation: () => ColorPickerFoundation; getDOM: () => HTMLDivElement } class AlphaSliderFoundation extends BaseFoundation, AlphaSliderBaseProps, AlphaSliderBaseState> { constructor(adapter: AlphaSliderAdapter) { super({ ...adapter }); } handleMouseDown = (e: any) => { this._adapter.handleMouseDown(e); } handleMouseUp = (e: any) => { this._adapter.handleMouseUp(e); } setHandlePositionByMousePosition = (e: MouseEvent) => { const rect = this._adapter.getDOM()?.getBoundingClientRect(); if (!rect) { return; } const { width, handleSize } = this._adapter.getProps(); const colorPickerFoundation = this._adapter.getColorPickerFoundation(); const mousePosition = e.clientX - rect.x; const handlePosition = colorPickerFoundation.getAlphaHandlePositionByMousePosition(mousePosition, width, handleSize); colorPickerFoundation.handleAlphaChangeByHandle({ a: Number((Math.min(Math.max(mousePosition / width, 0), 1)).toFixed(2)) }); this.setState({ handlePosition }); } } export default AlphaSliderFoundation;