UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

223 lines (220 loc) • 9.75 kB
import { jsx } from "react/jsx-runtime"; import { createContext, useContext, useState } from "react"; import { Platform } from "react-native"; import { useControlled } from "./controlled.native.js"; import NativeSheet from "./specs/OneNativeSheetNativeComponent.native.js"; import NativeContent from "./specs/OneNativeSheetContentNativeComponent.native.js"; import { assertSwiftUIValue } from "./generated/swiftui.native.js"; var DEFAULT_DETENTS = ["large"]; var SheetAppearanceContext = /* @__PURE__ */ createContext(void 0); function nativeDetent(detent) { if (detent === "medium" || detent === "large") return { type: detent, value: 0 }; if (typeof detent !== "object" || detent === null || Object.keys(detent).length !== 1) throw new Error("Invalid presentation detent"); if ("fraction" in detent && Number.isFinite(detent.fraction) && detent.fraction > 0 && detent.fraction <= 1) return { type: "fraction", value: detent.fraction }; if ("height" in detent && Number.isFinite(detent.height) && detent.height > 0) return { type: "height", value: detent.height }; throw new Error("Presentation detent must be medium, large, a fraction in (0, 1], or a positive height"); } function publicDetent(param) { var { type, value } = param; if (type === "medium" || type === "large") return type; if (type === "fraction") return { fraction: value }; if (type === "height") return { height: value }; throw new Error(`Unknown native presentation detent: ${type}`); } function sameDetent(a, b) { return a.type === b.type && a.value === b.value; } function usePresentationId(isPresented) { var [cycle, setCycle] = useState({ isPresented, id: isPresented ? 1 : 0 }); if (cycle.isPresented === isPresented) return cycle.id; var next = { isPresented, id: cycle.id + (isPresented ? 1 : 0) }; setCycle(next); return next.id; } function Sheet(param) { var { isPresented, onIsPresentedChange, onDismiss, onDidDismiss, revision = 0, presentationDetents, fitToContents = false, selectedDetent, onSelectedDetentChange, detentRevision = 0, presentationDragIndicator = "automatic", interactiveDismissDisabled = false, presentationBackground, presentationBackgroundInteraction = "automatic", presentationContentInteraction = "automatic", presentationSizing = "automatic", children, style, ...props } = param; var _ref, _ref1; var colorScheme = useContext(SheetAppearanceContext); if (typeof isPresented !== "boolean") throw new Error("Swift.Sheet isPresented must be a boolean"); var presentationId = usePresentationId(isPresented); var requestedDetents = presentationDetents !== null && presentationDetents !== void 0 ? presentationDetents : DEFAULT_DETENTS; if (!Array.isArray(requestedDetents) || !requestedDetents.length) throw new Error("Swift.Sheet requires at least one presentation detent"); var detents = requestedDetents.map(nativeDetent); if (new Set(detents.map(function(param2) { var { type, value } = param2; return `${type}:${value}`; })).size !== detents.length) throw new Error("Swift.Sheet presentation detents must be unique"); if (selectedDetent === void 0 !== (onSelectedDetentChange === void 0)) throw new Error("Swift.Sheet selectedDetent and onSelectedDetentChange must be provided together"); if (fitToContents && selectedDetent !== void 0) throw new Error("Swift.Sheet fitToContents cannot be combined with selectedDetent"); var nativeSelection = selectedDetent ? nativeDetent(selectedDetent) : null; if (nativeSelection && !detents.some(function(detent) { return sameDetent(detent, nativeSelection); })) throw new Error("Swift.Sheet selectedDetent must be in presentationDetents"); var backgroundInteraction = (function() { if (typeof presentationBackgroundInteraction === "string") { if (![ "automatic", "enabled", "disabled" ].includes(presentationBackgroundInteraction)) throw new Error("Invalid Swift.Sheet presentationBackgroundInteraction"); return { mode: presentationBackgroundInteraction, detent: { type: "large", value: 0 } }; } if (typeof presentationBackgroundInteraction !== "object" || presentationBackgroundInteraction === null || !("enabledUpThrough" in presentationBackgroundInteraction)) throw new Error("Invalid Swift.Sheet presentationBackgroundInteraction"); var detent = nativeDetent(presentationBackgroundInteraction.enabledUpThrough); if (fitToContents) throw new Error("Swift.Sheet fitToContents cannot use an enabledUpThrough background interaction"); if (!detents.some(function(candidate) { return sameDetent(candidate, detent); })) throw new Error("Swift.Sheet presentationBackgroundInteraction detent must be in presentationDetents"); return { mode: "enabledUpThrough", detent }; })(); if (![ "automatic", "fitted", "form", "page" ].includes(presentationSizing)) throw new Error("Invalid Swift.Sheet presentationSizing"); assertSwiftUIValue("Visibility", presentationDragIndicator, Number.parseFloat(String(Platform.Version))); assertSwiftUIValue("PresentationContentInteraction", presentationContentInteraction, Number.parseFloat(String(Platform.Version))); var controlled = useControlled(function(event) { return onIsPresentedChange(event.isPresented); }, revision); var controlledDetent = useControlled(function(event) { return onSelectedDetentChange === null || onSelectedDetentChange === void 0 ? void 0 : onSelectedDetentChange(publicDetent(event)); }, detentRevision); return /* @__PURE__ */ jsx(NativeSheet, { ...props, style: [{ position: "absolute", width: 0, height: 0 }, style], isPresented, revision, presentationId, acknowledgedEvent: controlled.acknowledgedEvent, detents, fitToContents, selectedDetentType: (_ref = nativeSelection === null || nativeSelection === void 0 ? void 0 : nativeSelection.type) !== null && _ref !== void 0 ? _ref : "", selectedDetentValue: (_ref1 = nativeSelection === null || nativeSelection === void 0 ? void 0 : nativeSelection.value) !== null && _ref1 !== void 0 ? _ref1 : 0, acknowledgedDetentEvent: controlledDetent.acknowledgedEvent, detentRevision, interactiveDismissDisabled, presentationDragIndicator, presentationBackground, presentationBackgroundInteraction: backgroundInteraction.mode, presentationBackgroundInteractionDetentType: backgroundInteraction.detent.type, presentationBackgroundInteractionDetentValue: backgroundInteraction.detent.value, presentationContentInteraction, presentationSizing, presentation: "sheet", colorScheme: colorScheme !== null && colorScheme !== void 0 ? colorScheme : "", onNativeSheetIsPresentedChange: function onNativeSheetIsPresentedChange(param2) { var { nativeEvent } = param2; return controlled.onNativeChange(nativeEvent); }, onNativeSheetDismiss: function onNativeSheetDismiss(param2) { var { nativeEvent } = param2; if (nativeEvent.revision === revision) onDismiss === null || onDismiss === void 0 ? void 0 : onDismiss(); }, onNativeSheetDidDismiss: function onNativeSheetDidDismiss(param2) { var { nativeEvent } = param2; if (!isPresented && nativeEvent.revision === revision && nativeEvent.presentationId === presentationId) onDidDismiss === null || onDidDismiss === void 0 ? void 0 : onDidDismiss(); }, onNativeSheetDetentChange: function onNativeSheetDetentChange(param2) { var { nativeEvent } = param2; return controlledDetent.onNativeChange(nativeEvent); }, children: /* @__PURE__ */ jsx(NativeContent, { collapsable: false, style: { position: "absolute", left: 0, top: 0 }, children }) }); } var COVER_DETENTS = []; function FullScreenCover(param) { var { isPresented, onIsPresentedChange, onDismiss, onDidDismiss, revision = 0, children, style, ...props } = param; if (typeof isPresented !== "boolean") throw new Error("Swift.FullScreenCover isPresented must be a boolean"); var presentationId = usePresentationId(isPresented); var controlled = useControlled(function(event) { return onIsPresentedChange(event.isPresented); }, revision); return /* @__PURE__ */ jsx(NativeSheet, { ...props, style: [{ position: "absolute", width: 0, height: 0 }, style], isPresented, revision, presentationId, acknowledgedEvent: controlled.acknowledgedEvent, detents: COVER_DETENTS, fitToContents: false, selectedDetentType: "", selectedDetentValue: 0, acknowledgedDetentEvent: 0, detentRevision: 0, presentationBackgroundInteraction: "automatic", presentationBackgroundInteractionDetentType: "large", presentationBackgroundInteractionDetentValue: 0, presentationContentInteraction: "automatic", presentationSizing: "automatic", interactiveDismissDisabled: false, presentationDragIndicator: "automatic", presentation: "fullScreenCover", colorScheme: "", onNativeSheetIsPresentedChange: function onNativeSheetIsPresentedChange(param2) { var { nativeEvent } = param2; return controlled.onNativeChange(nativeEvent); }, onNativeSheetDismiss: function onNativeSheetDismiss(param2) { var { nativeEvent } = param2; if (nativeEvent.revision === revision) onDismiss === null || onDismiss === void 0 ? void 0 : onDismiss(); }, onNativeSheetDidDismiss: function onNativeSheetDidDismiss(param2) { var { nativeEvent } = param2; if (!isPresented && nativeEvent.revision === revision && nativeEvent.presentationId === presentationId) onDidDismiss === null || onDidDismiss === void 0 ? void 0 : onDidDismiss(); }, children: /* @__PURE__ */ jsx(NativeContent, { collapsable: false, style: { position: "absolute", left: 0, top: 0 }, children }) }); } export { FullScreenCover, Sheet, SheetAppearanceContext }; //# sourceMappingURL=Sheet.native.js.map