if() Conditional Value
ridiculous/if-functionFreeComponent
Ridiculously typed editor for the CSS if() conditional value function. Validates the wrapper, splits branches, checks each condition kind (media/supports/style/else), and enforces else-last. Ships a popover branch-builder UI with a live preview.
Install it
npx shadcn@latest add http://ridiculous.turtlesocks.dev/r/if-function.jsonSource
1"use client"23import { useEffect, useRef, useState } from "react"4import { Button } from "@/components/ui/button"5import { Input } from "@/components/ui/input"6import {7 Popover,8 PopoverContent,9 PopoverTrigger,10} from "@/components/ui/popover"11import { cn } from "@/lib/utils"12import {13 branchCount,14 defaultBranch,15 formatIf,16 parseIf,17} from "./if-function.helpers"18import type {19 ConditionKind,20 IfBranch,21 IfFunctionString,22} from "./if-function.types"2324// ---------------------------------------------------------------------------25// Shared constants26// ---------------------------------------------------------------------------2728const ALL_KINDS: readonly ConditionKind[] = [29 "media",30 "supports",31 "style",32 "else",33]3435const CONDITION_PLACEHOLDER: Record<ConditionKind, string> = {36 media: "width >= 600px",37 supports: "display: grid",38 style: "--x: 1",39 else: "",40}4142// ---------------------------------------------------------------------------43// Props44// ---------------------------------------------------------------------------4546export interface IfFunctionPanelProps {47 value: IfFunctionString | (string & {})48 onChange: (value: IfFunctionString) => void49 className?: string50 "aria-label"?: string51}5253export type IfFunctionProps = IfFunctionPanelProps5455// ---------------------------------------------------------------------------56// IfFunction — popover-wrapped57// ---------------------------------------------------------------------------5859export function IfFunction(props: IfFunctionProps) {60 const {61 value,62 className,63 "aria-label": ariaLabel = "Edit a CSS if() conditional value",64 } = props65 const count = branchCount(String(value))6667 return (68 <Popover>69 <PopoverTrigger asChild>70 <Button71 variant="outline"72 className={cn("h-9 gap-2 px-3 font-mono", className)}73 aria-label={ariaLabel}74 >75 <span className="text-[10px] text-muted-foreground uppercase">76 if77 </span>78 <span className="text-[10px] text-muted-foreground">79 {count} {count === 1 ? "branch" : "branches"}80 </span>81 <span className="max-w-[200px] truncate text-xs">{value}</span>82 </Button>83 </PopoverTrigger>84 <PopoverContent className="w-auto p-0" align="start">85 <IfFunctionPanel {...props} />86 </PopoverContent>87 </Popover>88 )89}9091// ---------------------------------------------------------------------------92// … truncated
What it pulls in
Other registry items
Files it writes
More from ridiculous
All 22 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| All ridiculous componentsall | ridiculous | Components | Free | no deps | |
| Anchor Position Editoranchor-position-editor | ridiculous | Components | Free | no deps · 9 files | |
| Background Editorbackground-editor | ridiculous | Components | Free | no deps · 9 files | |
| Box Shadow Editorbox-shadow-editor | ridiculous | Components | Free | no deps · 7 files | |
| Calc Editorcalc-editor | ridiculous | Components | Free | no deps · 4 files | |
| Clip Path Editorclip-path-editor | ridiculous | Components | Free | no deps · 20 files | |
| Color Functioncolor-function | ridiculous | Components | Free | no deps · 10 files | |
| Color Pickercolor-picker | ridiculous | Components | Free | no deps · 11 files |
