Skip to main content

priv/registry/switch.json

{
  "files": [
    {
      "content": "defmodule Shadix.Components.Switch do\n  @moduledoc \"\"\"\n  Switch component translated from shadcn/ui (new-york-v4).\n\n  A field-aware boolean control rendered as a native checkbox styled into a\n  pill-shaped switch. Like the checkbox control, it emits a hidden `\"false\"`\n  input first so an unchecked switch still submits a value. The thumb is drawn\n  with a `before:` pseudo-element and translated on `checked:` — no JavaScript,\n  no `phx-*` bindings, no Radix `data-state` behavior.\n\n  Caller-supplied `class` is appended last; Tailwind cascade layers ensure it\n  wins over the defaults.\n  \"\"\"\n  use Phoenix.Component\n\n  import Shadix.Cn\n  import Shadix.Form\n\n  @base \"peer relative inline-flex h-5 w-9 shrink-0 cursor-pointer appearance-none items-center rounded-full border border-transparent bg-input shadow-xs transition-all motion-reduce:transition-none outline-none before:pointer-events-none before:absolute before:top-1/2 before:left-0.5 before:-translate-y-1/2 before:size-4 before:rounded-full before:bg-background before:transition-transform before:motion-reduce:transition-none before:content-[''] checked:bg-primary dark:checked:bg-primary checked:before:translate-x-4 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:bg-input/80 dark:aria-invalid:ring-destructive/40\"\n\n  attr(:field, Phoenix.HTML.FormField, required: true)\n  attr(:class, :string, default: nil)\n  attr(:rest, :global, include: ~w(disabled required readonly autofocus))\n\n  def switch(assigns) do\n    %{id: id, name: name, value: value, errors: errors} = field_attrs(assigns.field)\n    assigns = assign(assigns, id: id, name: name, value: value, errors: errors)\n    assigns = assign(assigns, :checked, Phoenix.HTML.Form.normalize_value(\"checkbox\", value))\n    assigns = assign(assigns, :computed_class, cn([@base, assigns.class]))\n\n    ~H\"\"\"\n    <input type=\"hidden\" name={@name} value=\"false\" />\n    <input\n      type=\"checkbox\"\n      role=\"switch\"\n      data-slot=\"switch\"\n      id={@id}\n      name={@name}\n      value=\"true\"\n      checked={@checked}\n      aria-invalid={(@errors != [] && \"true\") || nil}\n      aria-describedby={(@errors != [] && \"#{@id}-error\") || nil}\n      class={@computed_class}\n      {@rest}\n    />\n    <p\n      :if={@errors != []}\n      id={\"#{@id}-error\"}\n      data-slot=\"form-message\"\n      class=\"text-destructive text-sm mt-1\"\n    >\n      {List.first(@errors)}\n    </p>\n    \"\"\"\n  end\nend\n",
      "path": "switch.ex"
    }
  ],
  "hooks": [],
  "name": "switch",
  "npm_deps": [],
  "registry_deps": [
    "cn",
    "form"
  ]
}