11import { useId , useEffect , useRef } from "react" ;
2+ import PropTypes from "prop-types" ;
23import Coloris from "@melloware/coloris" ;
34import setter from "../setter" ;
45import * as Styled from "./styles" ;
56
6- function ColorInput ( { defaultValue, ...props } ) {
7+ function ColorInput ( { defaultValue, container , ...props } ) {
78 const id = useId ( ) ;
89 const inputRef = useRef ( ) ;
910 const colorRef = useRef ( props . value || defaultValue ) ;
1011
1112 const inputId = `color-input-${ id } ` ;
1213
1314 useEffect ( ( ) => {
14- Coloris . init ( ) ;
15+ Coloris . init ( { parent : container } ) ;
1516
1617 const setColor = event => {
1718 const isTarget = event . detail . currentEl . id === inputId ;
@@ -21,7 +22,7 @@ function ColorInput({ defaultValue, ...props }) {
2122 document . addEventListener ( "coloris:pick" , setColor ) ;
2223
2324 return ( ) => document . removeEventListener ( "coloris:pick" , setColor ) ;
24- } , [ inputId , props ] ) ;
25+ } , [ inputId , container , props ] ) ;
2526
2627 useEffect ( ( ) => {
2728 const onClose = ( ) => {
@@ -38,14 +39,15 @@ function ColorInput({ defaultValue, ...props }) {
3839 themeMode : "dark" ,
3940 clearButton : true ,
4041 defaultColor : defaultValue ,
41- margin : 5
42+ margin : 5 ,
43+ parent : container
4244 } ) ;
4345
4446 inputEl . addEventListener ( "close" , onClose ) ;
4547
4648 return ( ) => inputEl . removeEventListener ( "close" , onClose ) ;
4749 }
48- } , [ inputRef , defaultValue , colorRef , props ] ) ;
50+ } , [ inputRef , defaultValue , colorRef , container , props ] ) ;
4951
5052 return (
5153 < Styled . ColorInput
@@ -60,6 +62,14 @@ function ColorInput({ defaultValue, ...props }) {
6062 ) ;
6163}
6264
65+ ColorInput . propTypes = {
66+ defaultValue : PropTypes . string . isRequired ,
67+ // By default the color picker renders as a dialog in <body>.
68+ // Pass a selector if rendering in a focus trap.
69+ container : PropTypes . string
70+ // See BaseInput for remaining propTypes
71+ } ;
72+
6373export default setter ( ColorInput ) ;
6474
6575ColorInput . displayName = "Form.ColorInput" ;
0 commit comments