lightswind
Version:
A collection of beautifully crafted React Components, Blocks & Templates for Modern Developers. Create stunning web applications effortlessly by using our 160+ professional and animated react components.
156 lines • 8.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccordionContent = exports.AccordionTrigger = exports.AccordionItem = exports.Accordion = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const React = __importStar(require("react"));
const lucide_react_1 = require("lucide-react");
const utils_1 = require("@/components/lib/utils");
const AccordionContext = React.createContext(undefined);
const AccordionItemContext = React.createContext(undefined);
const Accordion = React.forwardRef(
// **MODIFICATION: Changed default `type` to "single" and included `collapsible`**
({ className, type = "single", value, defaultValue = [], onValueChange, collapsible = true, children, ...props }, ref) => {
// Helper function to ensure value is an array of strings
const normalizeValue = (val) => {
if (Array.isArray(val)) {
return val;
}
return val ? [val] : [];
};
// Initial state setup
const initialValues = normalizeValue(value !== undefined ? value : defaultValue);
// In "single" mode, ensure initial state only has one open item
const [values, setValues] = React.useState(type === "single" && initialValues.length > 1
? [initialValues[0]]
: initialValues);
// Controlled component logic: sync state with prop `value`
React.useEffect(() => {
if (value !== undefined) {
const newValues = normalizeValue(value);
setValues(type === "single" && newValues.length > 1
? [newValues[0]]
: newValues);
}
}, [value, type]);
const handleValueChange = React.useCallback((itemValue) => {
const isCurrentlyOpen = values.includes(itemValue);
let newValues = [];
if (type === "single") {
if (isCurrentlyOpen) {
// 1. Item is open. If collapsible is TRUE, close it (empty array).
// If collapsible is FALSE, it stays open (itemValue).
newValues = collapsible ? [] : [itemValue];
}
else {
// 2. Item is closed. Open it (new value), closing any old value.
newValues = [itemValue];
}
}
else {
// "multiple" type logic
newValues = isCurrentlyOpen
? values.filter((v) => v !== itemValue)
: [...values, itemValue];
}
// Update internal state if uncontrolled
if (value === undefined) {
setValues(newValues);
}
// Call external handler
onValueChange?.(newValues);
}, [values, onValueChange, value, type, collapsible]);
// Passed `type` and `collapsible` to the context
return ((0, jsx_runtime_1.jsx)(AccordionContext.Provider, { value: { value: values, onValueChange: handleValueChange, type, collapsible }, children: (0, jsx_runtime_1.jsx)("div", { ref: ref, className: (0, utils_1.cn)(className), ...props, children: children }) }));
});
exports.Accordion = Accordion;
Accordion.displayName = "Accordion";
const AccordionItem = React.forwardRef(({ className, value, disabled = false, children, ...props }, ref) => {
return ((0, jsx_runtime_1.jsx)(AccordionItemContext.Provider, { value: { value }, children: (0, jsx_runtime_1.jsx)("div", { ref: ref, className: (0, utils_1.cn)("border-b border-muted-foreground/20 text-black dark:text-white", className), "data-state": disabled ? "disabled" : undefined, "data-value": value, ...props, children: children }) }));
});
exports.AccordionItem = AccordionItem;
AccordionItem.displayName = "AccordionItem";
const AccordionTrigger = React.forwardRef(({ className, children, ...props }, ref) => {
const context = React.useContext(AccordionContext);
if (!context)
throw new Error("AccordionTrigger must be used within an Accordion");
const itemContext = React.useContext(AccordionItemContext);
if (!itemContext)
throw new Error("AccordionTrigger must be used within an AccordionItem");
const { value: values, onValueChange } = context;
const { value: itemValue } = itemContext;
const isOpen = values.includes(itemValue);
// Simplified handler: calls context function with its item value
const handleToggle = () => {
onValueChange(itemValue);
};
return ((0, jsx_runtime_1.jsxs)("button", { ref: ref, type: "button", className: (0, utils_1.cn)(`flex flex-1 items-center justify-between py-4 font-medium transition-all hover:underline`, className), onClick: handleToggle, "data-state": isOpen ? "open" : "closed", ...props, children: [children, (0, jsx_runtime_1.jsxs)("div", { className: "relative h-4 w-4 lg:h-6 lg:w-6 ml-2 shrink-0", children: [(0, jsx_runtime_1.jsx)(lucide_react_1.Plus, { className: (0, utils_1.cn)(` h-4 w-4 lg:h-6 lg:w-6 absolute transition-all duration-300
ease-in-out `,
// Rotates + Fades out when open
isOpen ? "opacity-0 rotate-90" : "opacity-100 rotate-0") }), (0, jsx_runtime_1.jsx)(lucide_react_1.Minus, { className: (0, utils_1.cn)(" h-4 w-4 lg:h-6 lg:w-6 absolute transition-all duration-300 ease-in-out",
// Fades in when open, starts rotated when closed
isOpen ? "opacity-100 rotate-0" : "opacity-0 -rotate-90") })] })] }));
});
exports.AccordionTrigger = AccordionTrigger;
AccordionTrigger.displayName = "AccordionTrigger";
const AccordionContent = React.forwardRef(({ className, children, ...props }, ref) => {
const context = React.useContext(AccordionContext);
if (!context)
throw new Error("AccordionContent must be used within an Accordion");
const itemContext = React.useContext(AccordionItemContext);
if (!itemContext)
throw new Error("AccordionContent must be used within an AccordionItem");
const { value: values } = context;
const { value: itemValue } = itemContext;
const isOpen = values.includes(itemValue);
const contentRef = React.useRef(null);
const [contentHeight, setContentHeight] = React.useState(0);
// Calculates the height of the content element for smooth opening/closing
React.useLayoutEffect(() => {
if (isOpen && contentRef.current) {
setContentHeight(contentRef.current.scrollHeight);
}
else {
setContentHeight(0);
}
}, [isOpen, children]); // Recalculate if open state changes or children change
return ((0, jsx_runtime_1.jsx)("div", { ref: ref, style: {
height: isOpen ? `${contentHeight}px` : "0px",
transition: "height 300ms cubic-bezier(0.4, 0, 0.2, 1)", // Smooth transition
}, className: "overflow-hidden", "data-state": isOpen ? "open" : "closed", ...props, children: (0, jsx_runtime_1.jsx)("div", { ref: contentRef, className: (0, utils_1.cn)("pb-4 pt-0 text-sm", className), children: children }) }));
});
exports.AccordionContent = AccordionContent;
AccordionContent.displayName = "AccordionContent";
//# sourceMappingURL=accordion.js.map