fix: prevent color picker jitter (#1981)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).

<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
Erwan Leboucher authored and GitHub committed 2026-08-28 10:24:17 +02:00
commit 8ba2d7750a
3 files changed
+27 -10

No files matched your search

+24 -4
View File
@@ -1,18 +1,32 @@
import FocusTrap from 'focus-trap-react';
import type { RectCords } from 'folds';
import { Box, Button, config, Menu, Text } from 'folds';
import { HexColorPicker } from 'react-colorful';
import { PopOut } from '$components/overlay-stack';
import type { MouseEventHandler, ReactNode } from 'react';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { stopPropagation } from '$utils/keyboard';
type HexColorPickerPopOutProps = {
children: (onOpen: MouseEventHandler<HTMLElement>, opened: boolean) => ReactNode;
picker: ReactNode;
color: string;
onChange: (color: string) => void;
onRemove?: () => void;
};
export function HexColorPickerPopOut({ picker, onRemove, children }: HexColorPickerPopOutProps) {
const isValidHexColor = (color: string) => /^#[0-9A-F]{6}$/i.test(color);
export function HexColorPickerPopOut({
color,
onChange,
onRemove,
children,
}: HexColorPickerPopOutProps) {
const [cords, setCords] = useState<RectCords>();
const [pickerColor, setPickerColor] = useState(isValidHexColor(color) ? color : '#FFFFFF');
useEffect(() => {
if (isValidHexColor(color)) setPickerColor(color);
}, [color]);
const handleOpen: MouseEventHandler<HTMLElement> = (evt) => {
setCords(evt.currentTarget.getBoundingClientRect());
@@ -39,7 +53,13 @@ export function HexColorPickerPopOut({ picker, onRemove, children }: HexColorPic
}}
>
<Box direction="Column" gap="200">
{picker}
<HexColorPicker
color={pickerColor}
onChange={(newColor) => {
setPickerColor(newColor);
onChange(newColor);
}}
/>
{onRemove && (
<Button
size="300"
@@ -26,7 +26,6 @@ import {
Trash,
X,
} from '$components/icons/phosphor';
import { HexColorPicker } from 'react-colorful';
import { useAtomValue } from 'jotai';
import { Page, PageContent, PageHeader } from '$components/page';
import type { IPowerLevels } from '$hooks/usePowerLevels';
@@ -134,7 +133,8 @@ function EditPower({ maxPower, power, tag, onSave, onClose }: Readonly<EditPower
<Text size="L400">Color</Text>
<Box gap="200" alignItems="Center">
<HexColorPickerPopOut
picker={<HexColorPicker color={tagColor} onChange={handleUpdateColor} />}
color={tagColor}
onChange={handleUpdateColor}
onRemove={() => setTagColor('#FFFFFF')}
>
{(openPicker, opened) => (
@@ -1,7 +1,6 @@
import { useState, useEffect } from 'react';
import { Box, Button, config, color, Text, Input, IconButton } from 'folds';
import { menuIcon, X } from '$components/icons/phosphor';
import { HexColorPicker } from 'react-colorful';
import { SettingTile } from '$components/setting-tile';
import { HexColorPickerPopOut } from '$components/HexColorPickerPopOut';
import { isValidHex } from '$hooks/useUserProfile';
@@ -116,9 +115,7 @@ export function NameColorEditor({
<Text size="B300">Save</Text>
</Button>
)}
<HexColorPickerPopOut
picker={<HexColorPicker color={tempColor ?? '#FFFFFF'} onChange={handleUpdate} />}
>
<HexColorPickerPopOut color={tempColor ?? '#FFFFFF'} onChange={handleUpdate}>
{(onOpen, opened) => (
<Button
onClick={onOpen}