Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(chrome-extension): replace microphone dropdown with device select overlay

Richie McIlroy7c2f345cb414
diff --git a/apps/chrome-extension/src/popup/components/microphone-selector.tsx b/apps/chrome-extension/src/popup/components/microphone-selector.tsx
index a1e766aee..94d8979a2 100644
--- a/apps/chrome-extension/src/popup/components/microphone-selector.tsx
+++ b/apps/chrome-extension/src/popup/components/microphone-selector.tsx
@@ -5,15 +5,13 @@ import {
import clsx from "clsx";
import { MicIcon, MicOffIcon } from "lucide-react";
import type { KeyboardEvent, MouseEvent } from "react";
+import { useRef } from "react";
import type { MicrophoneDevice } from "../../shared/types";
import { DEFAULT_MICROPHONE_DEVICE_ID } from "../../shared/types";
import {
- SelectContent,
- SelectItem,
- SelectRoot,
- SelectTrigger,
- SelectValue,
-} from "../ui/select";
+ type DeviceSelectOption,
+ DeviceSelectOverlay,
+} from "./device-select-overlay";
import { useMediaPermission } from "./use-media-permission";
interface MicrophoneSelectorProps {
@@ -33,13 +31,14 @@ export const MicrophoneSelector = ({
availableMics,
permissionGranted,
disabled = false,
- open,
+ open = false,
onOpenChange,
onMicChange,
onRefreshDevices,
onPermissionBlocked,
}: MicrophoneSelectorProps) => {
const micEnabled = selectedMicId !== null;
+ const triggerRef = useRef<HTMLButtonElement>(null);
const { state: permissionState, requestPermission } =
useMediaPermission("microphone");
@@ -52,6 +51,33 @@ export const MicrophoneSelector = ({
const statusPillDisabled =
disabled || (!shouldRequestPermission && !micEnabled);
+ const currentValue = selectedMicId ?? NO_MICROPHONE_VALUE;
+
+ const options: DeviceSelectOption[] = [
+ { value: NO_MICROPHONE_VALUE, label: NO_MICROPHONE, icon: MicOffIcon },
+ ];
+ if (
+ availableMics.length === 0 ||
+ selectedMicId === DEFAULT_MICROPHONE_DEVICE_ID
+ ) {
+ options.push({
+ value: DEFAULT_MICROPHONE_DEVICE_ID,
+ label: "Default microphone",
+ icon: MicIcon,
+ });
+ }
+ availableMics.forEach((mic, index) => {
+ options.push({
+ value: mic.deviceId,
+ label: mic.label?.trim() || `Microphone ${index + 1}`,
+ icon: MicIcon,
+ });
+ });
+
+ const selectedOption =
+ options.find((option) => option.value === currentValue) ?? options[0];
+ const TriggerIcon = selectedOption.icon;
+
const statusPillClassName = clsx(
"px-[0.375rem] h-[1.25rem] min-w-[2.5rem] rounded-full text-[0.75rem] leading-[1.25rem] flex items-center justify-center font-normal transition-colors duration-200 disabled:opacity-100 disabled:pointer-events-none focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-offset-1 focus-visible:ring-[var(--blue-8)]",
statusPillDisabled ? "cursor-default" : "cursor-pointer",
@@ -62,6 +88,16 @@ export const MicrophoneSelector = ({
: "bg-[var(--red-3)] text-[var(--red-11)]",
);
+ const openPicker = () => {
+ if (disabled || shouldRequestPermission) return;
+ onOpenChange?.(true);
+ };
+
+ const closePicker = () => {
+ onOpenChange?.(false);
+ triggerRef.current?.focus();
+ };
+
const handleStatusPillClick = async (
event: MouseEvent<HTMLButtonElement> | KeyboardEvent<HTMLButtonElement>,
) => {
@@ -114,89 +150,56 @@ export const MicrophoneSelector = ({
return (
<div className="flex flex-col gap-[0.25rem] items-stretch text-[--text-primary]">
- <SelectRoot
- value={selectedMicId ?? NO_MICROPHONE_VALUE}
- onValueChange={(value) => {
- onMicChange(value === NO_MICROPHONE_VALUE ? null : value);
- }}
- disabled={disabled}
- open={open}
- onOpenChange={onOpenChange}
- >
- <div className="relative w-full">
- <SelectTrigger
- className={clsx(
- "relative flex flex-row items-center h-[2rem] pl-[0.375rem] pr-[3.5rem] gap-[0.375rem] border border-gray-3 rounded-lg w-full transition-colors overflow-hidden z-10 font-normal text-[0.875rem] bg-transparent hover:bg-transparent focus:bg-transparent focus:border-gray-3 hover:border-gray-3 text-[--text-primary] disabled:text-gray-11 [&>svg]:hidden",
- disabled || shouldRequestPermission
- ? "cursor-default"
- : undefined,
- )}
- onPointerDown={(event) => {
- if (shouldRequestPermission) {
- event.preventDefault();
- event.stopPropagation();
- }
- }}
- onKeyDown={(event: KeyboardEvent<HTMLButtonElement>) => {
- if (shouldRequestPermission) {
- const keys = ["Enter", " ", "ArrowDown", "ArrowUp"];
- if (keys.includes(event.key)) {
- event.preventDefault();
- event.stopPropagation();
- }
- }
- }}
- aria-disabled={disabled || shouldRequestPermission}
- >
- <SelectValue
- placeholder={NO_MICROPHONE}
- className="flex-1 flex items-center gap-[0.375rem] truncate"
- />
- </SelectTrigger>
- <button
- type="button"
- className={clsx(
- statusPillClassName,
- "absolute right-[0.375rem] top-1/2 -translate-y-1/2 z-20",
- )}
- disabled={statusPillDisabled}
- aria-disabled={statusPillDisabled}
- onClick={(event) => void handleStatusPillClick(event)}
- onKeyDown={handleStatusPillKeyDown}
- >
- {shouldRequestPermission
- ? "Request permission"
- : micEnabled
- ? "On"
- : "Off"}
- </button>
- </div>
- <SelectContent className="z-[502]">
- <SelectItem value={NO_MICROPHONE_VALUE}>
- <span className="flex items-center gap-2 truncate">
- <MicOffIcon className="size-4 text-gray-11" />
- {NO_MICROPHONE}
- </span>
- </SelectItem>
- {(availableMics.length === 0 ||
- selectedMicId === DEFAULT_MICROPHONE_DEVICE_ID) && (
- <SelectItem value={DEFAULT_MICROPHONE_DEVICE_ID}>
- <span className="flex items-center gap-2 truncate">
- <MicIcon className="size-4 text-gray-11" />
- Default microphone
- </span>
- </SelectItem>
+ <div className="relative w-full">
+ <button
+ ref={triggerRef}
+ type="button"
+ disabled={disabled}
+ aria-haspopup="dialog"
+ aria-expanded={open}
+ onClick={openPicker}
+ className={clsx(
+ "relative flex flex-row items-center h-[2rem] pl-[0.375rem] pr-[3.5rem] gap-[0.375rem] border border-gray-3 rounded-lg w-full transition-colors overflow-hidden z-10 font-normal text-[0.875rem] bg-transparent hover:bg-transparent text-[--text-primary] disabled:text-gray-11",
+ disabled || shouldRequestPermission
+ ? "cursor-default"
+ : "cursor-pointer",
+ )}
+ >
+ <span className="flex-1 flex items-center gap-[0.375rem] truncate">
+ <TriggerIcon className="size-4 shrink-0 text-gray-11" aria-hidden />
+ <span className="truncate">{selectedOption.label}</span>
+ </span>
+ </button>
+ <button
+ type="button"
+ className={clsx(
+ statusPillClassName,
+ "absolute right-[0.375rem] top-1/2 -translate-y-1/2 z-20",
)}
- {availableMics.map((mic, index) => (
- <SelectItem key={mic.deviceId} value={mic.deviceId}>
- <span className="flex items-center gap-2 truncate">
- <MicIcon className="size-4 text-gray-11" />
- {mic.label?.trim() || `Microphone ${index + 1}`}
- </span>
- </SelectItem>
- ))}
- </SelectContent>
- </SelectRoot>
+ disabled={statusPillDisabled}
+ aria-disabled={statusPillDisabled}
+ onClick={(event) => void handleStatusPillClick(event)}
+ onKeyDown={handleStatusPillKeyDown}
+ >
+ {shouldRequestPermission
+ ? "Request permission"
+ : micEnabled
+ ? "On"
+ : "Off"}
+ </button>
+ </div>
+ {open && (
+ <DeviceSelectOverlay
+ title="Select microphone"
+ options={options}
+ selectedValue={currentValue}
+ onSelect={(value) => {
+ onMicChange(value === NO_MICROPHONE_VALUE ? null : value);
+ closePicker();
+ }}
+ onClose={closePicker}
+ />
+ )}
</div>
);
};