swap
diceui-radix/swapFreeComponent
diceui/radix publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/sadmann7/diceui/HEAD/docs/public/r/styles/radix-nova/swap.jsonSource
1"use client";23import { Slot as SlotPrimitive } from "radix-ui";4import * as React from "react";5import { cn } from "@/lib/utils";6import { useAsRef } from "@/registry/bases/radix/hooks/use-as-ref";7import { useIsomorphicLayoutEffect } from "@/registry/bases/radix/hooks/use-isomorphic-layout-effect";8import { useLazyRef } from "@/registry/bases/radix/hooks/use-lazy-ref";910interface DivProps extends React.ComponentProps<"div"> {11 asChild?: boolean;12}1314function getDataState(swapped: boolean) {15 return swapped ? "on" : "off";16}1718interface StoreState {19 swapped: boolean;20}2122interface Store {23 subscribe: (callback: () => void) => () => void;24 getState: () => StoreState;25 setState: <K extends keyof StoreState>(key: K, value: StoreState[K]) => void;26 notify: () => void;27}2829const StoreContext = React.createContext<Store | null>(null);3031function useStore<T>(32 selector: (state: StoreState) => T,33 ogStore?: Store | null,34): T {35 const contextStore = React.useContext(StoreContext);3637 const store = ogStore ?? contextStore;3839 if (!store) {40 throw new Error(`\`useStore\` must be used within \`Swap\``);41 }4243 const getSnapshot = React.useCallback(44 () => selector(store.getState()),45 [store, selector],46 );4748 return React.useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);49}5051interface SwapProps extends DivProps {52 swapped?: boolean;53 defaultSwapped?: boolean;54 onSwappedChange?: (swapped: boolean) => void;55 activationMode?: "click" | "hover";56 animation?: "fade" | "rotate" | "flip" | "scale";57 disabled?: boolean;58}5960function Swap(props: SwapProps) {61 const {62 swapped: swappedProp,63 defaultSwapped,64 onSwappedChange,65 activationMode = "click",66 animation = "fade",67 disabled,68 asChild,69 className,70 onClick: onClickProp,71 onMouseEnter: onMouseEnterProp,72 onMouseLeave: onMouseLeaveProp,73 onKeyDown: onKeyDownProp,74 ...rootProps75 } = props;7677 const listenersRef = useLazyRef(() => new Set<() => void>());78 const stateRef = useLazyRef<StoreState>(() => ({79 swapped: swappedProp ?? defaultSwapped ?? false,80 }));8182 const propsRef = useAsRef({83 activationMode,84 animation,85 disabled,86 onSwappedChange,87 onClick: onClickProp,88 onMouseEnter: onMouseEnterProp,89 onMouseLeave: onMouseLeaveProp,90 onKeyDown: onKeyDownProp,91 });9293 const isClickMode = activationMode === "click";9495 const store = React.useMemo<Store>(() => {96 return {97 subscribe: (cb) => {98// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from diceui/radix
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| action-baraction-bar | diceui/radix | Components | Free | 1 dep · 2 files | |
| angle-sliderangle-slider | diceui/radix | Components | Free | 1 dep · 3 files | |
| avatar-groupavatar-group | diceui/radix | Components | Free | 1 dep | |
| badge-overflowbadge-overflow | diceui/radix | Components | Free | 1 dep · 2 files | |
| bannerbanner | diceui/radix | Components | Free | 1 dep | |
| checkbox-groupcheckbox-group | diceui/radix | Components | Free | 1 dep | |
| circular-progresscircular-progress | diceui/radix | Components | Free | 1 dep | |
| client-onlyclient-only | diceui/radix | Components | Free | no deps |
