@crossed/ui
Version:
A universal & performant styling library for React Native, Next.js & React
187 lines (186 loc) • 6.91 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Accordion_exports = {};
__export(Accordion_exports, {
Accordion: () => Accordion,
AccordionIcon: () => AccordionIcon,
AccordionItem: () => AccordionItem,
AccordionPanel: () => AccordionPanel,
AccordionTrigger: () => AccordionTrigger
});
module.exports = __toCommonJS(Accordion_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_styled = require("@crossed/styled");
var import_react = require("react");
var import_unicons = require("@crossed/unicons");
var import_react_native = require("react-native");
var import_core = require("@crossed/core");
var import_Floating = require("../overlay/Floating");
var import_react_native_reanimated = __toESM(require("react-native-reanimated"));
var import_context = require("../overlay/Floating/context");
const accordionStyles = (0, import_styled.createStyles)((t) => ({
root: {
base: {
borderBottomWidth: 1,
borderColor: t.colors.border.primary,
borderStyle: "solid"
}
},
trigger: {
base: {
flexDirection: "row",
justifyContent: "space-between"
}
},
panel: {
web: {
base: { overflow: "hidden", transition: "height 170ms ease-out" }
}
},
item: {
base: {
borderTopWidth: 1,
borderColor: t.colors.border.primary,
borderStyle: "solid"
},
web: {
base: { transition: "height 1000ms ease" }
}
}
}));
const rootContext = (0, import_react.createContext)(
{}
);
const itemContext = (0, import_react.createContext)({});
const Root = (props) => {
const {
children,
allowMultiple = false,
defaultValues,
values: valueProps,
onChange
} = props;
const [values, setValues] = (0, import_core.useUncontrolled)({
defaultValue: defaultValues ?? [],
value: valueProps,
onChange
});
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(rootContext.Provider, { value: { values, setValues, allowMultiple }, children });
};
Root.displayName = "Accordion";
const AccordionItem = ({ children, value }) => {
const { setValues, values, allowMultiple } = (0, import_react.useContext)(rootContext);
const buttonId = (0, import_react.useRef)();
const panelId = (0, import_react.useRef)();
const handleChange = (0, import_react.useCallback)(() => {
setValues(
allowMultiple ? values.includes(value) ? values.filter((e) => e !== value) : [...values, value] : [value]
);
}, [setValues, value, values, allowMultiple]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(itemContext.Provider, { value: { value, buttonId, panelId }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Floating.Floating,
{
onChange: handleChange,
value: values.includes(value),
removeScroll: false,
children
}
) });
};
AccordionItem.displayName = "Accordion.Item";
const AccordionTrigger = (0, import_react.forwardRef)(
({ style, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Floating.Floating.Trigger,
{
...props,
ref,
style: (0, import_styled.composeStyles)(accordionStyles.trigger, style)
}
)
);
AccordionTrigger.displayName = "Accordion.Trigger";
const AccordionPanel = ({ children, style }) => {
const openSharedValue = (0, import_react_native_reanimated.useSharedValue)(false);
const { open } = (0, import_context.useFloatingContext)();
const height = (0, import_react_native_reanimated.useSharedValue)(0);
const handleLayout = (0, import_react.useCallback)(
(_w, h) => {
height.value = h;
},
[height]
);
(0, import_react.useEffect)(() => {
openSharedValue.value = open;
}, [open]);
const animatedStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => {
return {
height: (0, import_react_native_reanimated.withTiming)(openSharedValue.value ? height.value : 0)
};
}, [height, openSharedValue]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Floating.Floating.VisibilityHidden,
{
animatedStyle,
style: (0, import_styled.composeStyles)(
(0, import_styled.inlineStyle)(() => ({
base: { position: "relative" }
})),
style
),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.ScrollView, { onContentSizeChange: handleLayout, style: { flex: 1 }, children })
}
);
};
AccordionPanel.displayName = "Accordion.Panel";
const AccordionIcon = ({ style }) => {
const { value } = (0, import_react.useContext)(itemContext);
const { values } = (0, import_react.useContext)(rootContext);
const isOpen = values.includes(value);
const animatedStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => {
return {
transform: [{ rotate: (0, import_react_native_reanimated.withTiming)(isOpen ? "180deg" : "0deg") }]
};
}, [isOpen]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { style: [style == null ? void 0 : style.style().style, animatedStyle], children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_unicons.ChevronDown, {}) });
};
AccordionIcon.displayName = "Accordion.Icon";
const Accordion = (0, import_core.withStaticProperties)(Root, {
Item: AccordionItem,
Panel: AccordionPanel,
Trigger: AccordionTrigger,
Icon: AccordionIcon
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Accordion,
AccordionIcon,
AccordionItem,
AccordionPanel,
AccordionTrigger
});
//# sourceMappingURL=Accordion.js.map