Skip to content

Commit

Permalink
fix: bubble events
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Oct 12, 2024
1 parent 5ecbb4f commit db0b401
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@

.input::placeholder {
font-family: var(--cui-font-stack-mono);
}
}
17 changes: 15 additions & 2 deletions packages/circuit-ui/components/ColorInput/ColorInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,23 @@ export const ColorInput = forwardRef<InputElement, ColorInputProps>(
return;
}

colorPickerRef.current.value = pastedText.startsWith('#')
const pastedColor = pastedText.startsWith('#')
? pastedText
: `#${pastedText}`;
colorInputRef.current.value = pastedText.replace('#', '');

colorPickerRef.current.value = pastedColor;

// React overwrites the input.value setter. In order to be able to trigger
// a 'change' event on the input, we need to use the native setter.
// Adapted from https://stackoverflow.com/a/46012210/4620154
Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
)?.set?.call(colorInputRef.current, pastedColor.replace('#', ''));

colorInputRef.current.dispatchEvent(
new Event('change', { bubbles: true }),
);
colorPickerRef.current.dispatchEvent(
new Event('change', { bubbles: true }),
);
Expand Down

0 comments on commit db0b401

Please sign in to comment.