Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Extract ColorPickerWithSaveButton shared by both settings pages (#2928)

Luke Vella7b2df6bd3731
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index 430a32572..77ab45739 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -129,6 +129,7 @@
"changeRole": "Change role",
"charactersRemaining": "{count, plural, one {# character remaining} other {# characters remaining}}",
"checkForConflicts": "Toggle which calendars to check for conflicts",
+ "chooseColor": "Choose color",
"chooseImage": "Choose…",
"cityTime": "{city} time",
"clockPreferences": "Clock preferences",
@@ -338,6 +339,7 @@
"guest": "Guest",
"guestPollAlertDescription": "<0>Create an account</0> or <1>login</1> to claim this poll.",
"guestPollAlertTitle": "Your administrator rights can be lost if you clear your cookies",
+ "hexColor": "Hex color",
"hiddenParticipantName": "Participant #{number}",
"hideAttribution": "Hide attribution",
"hideParticipantsDescription": "Participants will not be able to see the names of other respondents.",
@@ -623,12 +625,12 @@
"requiredString": "“{name}” is required",
"requireParticipantEmailDescription": "Participants must provide an email address to respond.",
"requireParticipantEmailTitle": "Require email",
+ "reset": "Reset",
"resetPasswordButton": "Reset password",
"resetPasswordDescription": "Enter your new password below",
"resetPasswordFooter": "Don't need to reset? <a>Back to login</a>",
"resetPasswordInvalidTokenMessage": "The password reset link is invalid or has expired. Please request a new one.",
"resetPasswordTitle": "Reset password",
- "resetToDefault": "Reset to default",
"respondedOn": "Responded on",
"response": "Response",
"responseOptions": "Response options",
diff --git a/apps/web/src/app/[locale]/(space)/settings/general/components/custom-branding-section.tsx b/apps/web/src/app/[locale]/(space)/settings/general/components/custom-branding-section.tsx
index 4ffe9b800..d84bf7bfc 100644
--- a/apps/web/src/app/[locale]/(space)/settings/general/components/custom-branding-section.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/general/components/custom-branding-section.tsx
@@ -1,7 +1,6 @@
"use client";
-import type { Color } from "@rallly/ui/color-picker";
-import { ColorPicker, parseColor } from "@rallly/ui/color-picker";
+import { parseColor } from "@rallly/ui/color-picker";
import {
Field,
FieldContent,
@@ -10,12 +9,10 @@ import {
FieldLabel,
FieldTitle,
} from "@rallly/ui/field";
-import { InputGroupButton } from "@rallly/ui/input-group";
import { toast } from "@rallly/ui/sonner";
import { Switch } from "@rallly/ui/switch";
-import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";
-import { CheckIcon, RotateCcwIcon } from "lucide-react";
import React from "react";
+import { ColorPickerWithSaveButton } from "@/components/color-picker-with-save-button";
import { IfCloudHosted } from "@/components/environment";
import {
PageSection,
@@ -52,8 +49,6 @@ export function CustomBrandingSection({
const currentColor = space.primaryColor ?? DEFAULT_PRIMARY_COLOR;
const [color, setColor] = React.useState(() => parseColor(currentColor));
const hexColor = color.toString("hex");
- // Stored values may differ in case from what the picker emits
- const isDirty = hexColor.toLowerCase() !== currentColor.toLowerCase();
const isDefault =
hexColor.toLowerCase() === DEFAULT_PRIMARY_COLOR.toLowerCase();
@@ -119,7 +114,7 @@ export function CustomBrandingSection({
<SpaceSettingsForm space={space} disabled={disabled} />
<Field orientation="responsive">
<FieldContent>
- <FieldTitle>
+ <FieldTitle id="primary-color-label">
<Trans
i18nKey="primaryColorSettingTitle"
defaults="Primary color"
@@ -132,15 +127,15 @@ export function CustomBrandingSection({
/>
</FieldDescription>
</FieldContent>
- <PrimaryColorField
+ <ColorPickerWithSaveButton
value={color}
onChange={setColor}
disabled={disabled}
- isDefault={isDefault}
- isDirty={isDirty}
isSaving={updateSpace.isExecuting}
onSave={handleSave}
onReset={handleReset}
+ showReset={!isDefault}
+ aria-labelledby="primary-color-label"
/>
</Field>
<IfCloudHosted>
@@ -182,78 +177,3 @@ export function CustomBrandingSection({
</PageSection>
);
}
-
-function PrimaryColorField({
- value,
- onChange,
- disabled,
- isDefault,
- isDirty,
- isSaving,
- onSave,
- onReset,
-}: {
- value: Color;
- onChange: (color: Color) => void;
- disabled: boolean;
- isDefault: boolean;
- isDirty: boolean;
- isSaving: boolean;
- onSave: () => void;
- onReset: () => void;
-}) {
- const { t } = useTranslation();
-
- return (
- <ColorPicker
- className="w-44"
- value={value}
- onChange={onChange}
- actions={
- disabled ? null : (
- <>
- {!isDefault ? (
- <Tooltip>
- <TooltipTrigger
- render={
- <InputGroupButton
- size="icon-xs"
- onClick={onReset}
- aria-label={t("resetToDefault", {
- defaultValue: "Reset to default",
- })}
- >
- <RotateCcwIcon />
- </InputGroupButton>
- }
- />
- <TooltipContent>
- <Trans i18nKey="resetToDefault" defaults="Reset to default" />
- </TooltipContent>
- </Tooltip>
- ) : null}
- {isDirty ? (
- <Tooltip>
- <TooltipTrigger
- render={
- <InputGroupButton
- size="icon-xs"
- onClick={onSave}
- loading={isSaving}
- aria-label={t("save", { defaultValue: "Save" })}
- >
- <CheckIcon />
- </InputGroupButton>
- }
- />
- <TooltipContent>
- <Trans i18nKey="save" defaults="Save" />
- </TooltipContent>
- </Tooltip>
- ) : null}
- </>
- )
- }
- />
- );
-}
diff --git a/apps/web/src/app/[locale]/control-panel/branding/components/primary-color-field.tsx b/apps/web/src/app/[locale]/control-panel/branding/components/primary-color-field.tsx
index 2e7f494cf..31fa2a166 100644
--- a/apps/web/src/app/[locale]/control-panel/branding/components/primary-color-field.tsx
+++ b/apps/web/src/app/[locale]/control-panel/branding/components/primary-color-field.tsx
@@ -1,12 +1,10 @@
"use client";
-import { ColorPicker, parseColor } from "@rallly/ui/color-picker";
-import { InputGroupButton } from "@rallly/ui/input-group";
+import { parseColor } from "@rallly/ui/color-picker";
import { toast } from "@rallly/ui/sonner";
-import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";
-import { CheckIcon } from "lucide-react";
import React from "react";
-import { Trans, useTranslation } from "@/i18n/client";
+import { ColorPickerWithSaveButton } from "@/components/color-picker-with-save-button";
+import { useTranslation } from "@/i18n/client";
import { useSafeAction } from "@/lib/safe-action/client";
import { updateBrandingSettingsAction } from "../actions";
@@ -25,21 +23,6 @@ export function PrimaryColorField({
const updateBranding = useSafeAction(updateBrandingSettingsAction);
const [color, setColor] = React.useState(() => parseColor(defaultValue));
const hexColor = color.toString("hex");
- // Stored values may differ in case from what the picker emits
- const isDirty = hexColor.toLowerCase() !== defaultValue.toLowerCase();
-
- if (disabled) {
- // The color picker has no disabled state; show a static swatch instead
- return (
- <div className="flex items-center gap-2">
- <div
- className="size-8 rounded border"
- style={{ backgroundColor: defaultValue }}
- />
- <span className="font-mono text-sm">{defaultValue}</span>
- </div>
- );
- }
const handleSave = async () => {
const result = await updateBranding.executeAsync(
@@ -54,32 +37,13 @@ export function PrimaryColorField({
};
return (
- <ColorPicker
- className="w-44"
+ <ColorPickerWithSaveButton
value={color}
onChange={setColor}
+ disabled={disabled}
+ isSaving={updateBranding.isExecuting}
+ onSave={handleSave}
aria-labelledby={labelledBy}
- actions={
- isDirty ? (
- <Tooltip>
- <TooltipTrigger
- render={
- <InputGroupButton
- size="icon-xs"
- onClick={handleSave}
- loading={updateBranding.isExecuting}
- aria-label={t("save", { defaultValue: "Save" })}
- >
- <CheckIcon />
- </InputGroupButton>
- }
- />
- <TooltipContent>
- <Trans i18nKey="save" defaults="Save" />
- </TooltipContent>
- </Tooltip>
- ) : null
- }
/>
);
}
diff --git a/apps/web/src/components/color-picker-with-save-button.tsx b/apps/web/src/components/color-picker-with-save-button.tsx
new file mode 100644
index 000000000..81392cb75
--- /dev/null
+++ b/apps/web/src/components/color-picker-with-save-button.tsx
@@ -0,0 +1,77 @@
+"use client";
+
+import { Button } from "@rallly/ui/button";
+import type { Color } from "@rallly/ui/color-picker";
+import { ColorPicker } from "@rallly/ui/color-picker";
+import { Trans, useTranslation } from "@/i18n/client";
+
+/**
+ * Color swatch that opens a picker popover with save and optional
+ * reset-to-default actions in its footer (pass `onReset` to show the
+ * reset button; `showReset` enables it).
+ *
+ * The wrapper div matters: react-aria's ColorPicker renders no element of
+ * its own, so without it the swatch trigger would be a direct child of
+ * Field, whose `*:w-auto` child selector collapses the button's width.
+ */
+export function ColorPickerWithSaveButton({
+ value,
+ onChange,
+ isSaving,
+ onSave,
+ onReset,
+ showReset = false,
+ disabled = false,
+ "aria-labelledby": labelledBy,
+}: {
+ value: Color;
+ onChange: (color: Color) => void;
+ isSaving: boolean;
+ onSave: () => void;
+ onReset?: () => void;
+ showReset?: boolean;
+ disabled?: boolean;
+ "aria-labelledby"?: string;
+}) {
+ const { t } = useTranslation();
+
+ return (
+ <div>
+ <ColorPicker
+ value={value}
+ onChange={onChange}
+ disabled={disabled}
+ aria-labelledby={labelledBy}
+ aria-label={t("chooseColor", { defaultValue: "Choose color" })}
+ hexInputLabel={t("hexColor", { defaultValue: "Hex color" })}
+ footer={
+ <div className="flex justify-end gap-2">
+ {onReset ? (
+ <Button
+ size="sm"
+ variant="ghost"
+ onClick={onReset}
+ disabled={!showReset || disabled || isSaving}
+ >
+ <Trans i18nKey="reset" defaults="Reset" />
+ </Button>
+ ) : null}
+ {/* Not gated on isDirty: the hex field only commits on blur, so a
+ typed color hasn't reached `value` yet when the user clicks —
+ the click's mousedown commits it, then the handler saves. */}
+ <Button
+ size="sm"
+ variant="default"
+ className="flex-1"
+ onClick={onSave}
+ loading={isSaving}
+ disabled={disabled || isSaving}
+ >
+ <Trans i18nKey="save" defaults="Save" />
+ </Button>
+ </div>
+ }
+ />
+ </div>
+ );
+}
diff --git a/packages/ui/src/color-picker.tsx b/packages/ui/src/color-picker.tsx
index 100e721a2..cb2ae4dcb 100644
--- a/packages/ui/src/color-picker.tsx
+++ b/packages/ui/src/color-picker.tsx
@@ -16,12 +16,8 @@ import {
ColorThumb,
SliderTrack,
} from "react-aria-components";
-import {
- InputGroup,
- InputGroupAddon,
- InputGroupButton,
- InputGroupInput,
-} from "./input-group";
+import { Button } from "./button";
+import { Input } from "./input";
import { cn } from "./lib/utils";
import { Popover, PopoverContent, PopoverTrigger } from "./popover";
@@ -29,10 +25,18 @@ export type ColorPickerProps = Pick<
RAColorPickerProps,
"value" | "onChange" | "defaultValue"
> & {
- /** Rendered as an inline-end addon, for actions such as save or reset. */
- actions?: React.ReactNode;
className?: string;
- /** Applied to the hex input, which is the control an external label names. */
+ disabled?: boolean;
+ /** Rendered below the hex input, for actions such as save or reset. */
+ footer?: React.ReactNode;
+ /** Localized accessible name for the hex input. */
+ hexInputLabel?: string;
+ /**
+ * Localized fallback name for the swatch trigger, used when no
+ * `aria-labelledby` is given.
+ */
+ "aria-label"?: string;
+ /** Applied to the swatch trigger, which is the control an external label names. */
"aria-labelledby"?: string;
"aria-describedby"?: string;
};
@@ -41,11 +45,11 @@ export type { Color };
export { parseColor } from "react-aria-components";
function HexColorInput({
- "aria-labelledby": labelledBy,
- "aria-describedby": describedBy,
+ disabled,
+ "aria-label": label,
}: {
- "aria-labelledby"?: string;
- "aria-describedby"?: string;
+ disabled?: boolean;
+ "aria-label": string;
}) {
const pickerState = React.useContext(ColorPickerStateContext);
const inputRef = React.useRef<HTMLInputElement>(null);
@@ -58,28 +62,26 @@ function HexColorInput({
});
const { inputProps } = useColorField(
- // An external label wins; the generic fallback only applies when the
- // picker is used without one.
- labelledBy
- ? { "aria-labelledby": labelledBy, "aria-describedby": describedBy }
- : { "aria-label": "Hex color", "aria-describedby": describedBy },
+ { "aria-label": label, isDisabled: disabled },
state,
inputRef,
);
return (
- <InputGroupInput
+ <Input
{...inputProps}
ref={inputRef}
- data-slot="input-group-control"
- className="flex-1 rounded-none border-0 bg-transparent font-mono text-sm uppercase shadow-none outline-none ring-0 focus-visible:ring-0"
+ className="mt-2 h-8 w-full font-mono text-sm uppercase"
/>
);
}
export function ColorPicker({
- actions,
className,
+ disabled,
+ footer,
+ hexInputLabel = "Hex color",
+ "aria-label": label = "Choose color",
"aria-labelledby": labelledBy,
"aria-describedby": describedBy,
...props
@@ -87,25 +89,27 @@ export function ColorPicker({
return (
<ColorPickerPrimitive {...props}>
<Popover>
- <InputGroup className={cn("w-32", className)}>
- <InputGroupAddon align="inline-start">
- <PopoverTrigger render={<InputGroupButton />}>
- <ColorSwatch className="size-4 rounded-sm border border-black/10" />
- </PopoverTrigger>
- </InputGroupAddon>
- <HexColorInput
- aria-labelledby={labelledBy}
- aria-describedby={describedBy}
- />
- {actions ? (
- <InputGroupAddon align="inline-end">{actions}</InputGroupAddon>
- ) : null}
- </InputGroup>
- <PopoverContent align="start" className="w-48">
+ <PopoverTrigger
+ render={
+ <Button
+ variant="default"
+ size="icon"
+ disabled={disabled}
+ className={className}
+ aria-labelledby={labelledBy}
+ aria-describedby={describedBy}
+ {...(labelledBy ? {} : { "aria-label": label })}
+ />
+ }
+ >
+ <ColorSwatch className="size-5 rounded-sm border border-black/10" />
+ </PopoverTrigger>
+ <PopoverContent align="end" className="w-48">
<ColorArea
colorSpace="hsb"
xChannel="saturation"
yChannel="brightness"
+ isDisabled={disabled}
className="w-full rounded-md"
style={{ height: 148 }}
>
@@ -116,7 +120,12 @@ export function ColorPicker({
)}
/>
</ColorArea>
- <ColorSlider colorSpace="hsb" channel="hue" className="w-full">
+ <ColorSlider
+ colorSpace="hsb"
+ channel="hue"
+ isDisabled={disabled}
+ className="w-full"
+ >
<SliderTrack className="mt-2 h-3 w-full rounded-full">
<ColorThumb
className={cn(
@@ -126,6 +135,8 @@ export function ColorPicker({
/>
</SliderTrack>
</ColorSlider>
+ <HexColorInput disabled={disabled} aria-label={hexInputLabel} />
+ {footer ? <div className="mt-2">{footer}</div> : null}
</PopoverContent>
</Popover>
</ColorPickerPrimitive>
diff --git a/packages/ui/src/switch.tsx b/packages/ui/src/switch.tsx
index ff70d8fdb..99d5e950a 100644
--- a/packages/ui/src/switch.tsx
+++ b/packages/ui/src/switch.tsx
@@ -23,14 +23,14 @@ function Switch({
nativeButton
data-slot="switch"
className={cn(
- "peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full p-0.5 ring-1 ring-button-outline ring-inset transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-offset-2 data-disabled:cursor-not-allowed data-checked:bg-primary data-unchecked:bg-muted data-disabled:opacity-50",
+ "peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full p-[3px] ring-1 ring-button-outline ring-inset transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-offset-2 data-disabled:cursor-not-allowed data-checked:bg-primary data-unchecked:bg-muted data-disabled:opacity-50",
className,
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
- className="pointer-events-none flex h-full w-4 items-center justify-center rounded-full bg-background shadow-lg ring-0 transition-transform duration-100 data-checked:translate-x-4 data-unchecked:translate-x-0 dark:bg-gray-700"
+ className="pointer-events-none flex h-full w-3.5 items-center justify-center rounded-full border border-black/10 bg-background shadow-sm ring-0 transition-transform duration-100 data-checked:translate-x-4 data-unchecked:translate-x-0 dark:bg-gray-700"
>
{icon}
</SwitchPrimitive.Thumb>