diff --git a/package.json b/package.json index 63975a7..6ab3bec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/twui", - "version": "0.1.14", + "version": "0.1.15", "description": "A modular skeletal React component framework for Tailwind CSS v4", "author": "Benjamin Toby", "type": "module", diff --git a/src/(functions)/popver/grab-popover-styles.ts b/src/(functions)/popver/grab-popover-styles.ts index a2afeca..8fbdcbb 100644 --- a/src/(functions)/popver/grab-popover-styles.ts +++ b/src/(functions)/popver/grab-popover-styles.ts @@ -1,6 +1,5 @@ -import merge from "lodash/merge"; -import omit from "lodash/omit"; import React from "react"; +import twuiOmit from "../../utils/omit"; import { TWUIPopoverStyles } from "../../elements/Modal"; import twuiNumberfy from "../../utils/numberfy"; @@ -59,29 +58,25 @@ export default function twuiGrabPopoverStyles({ }; if (position === "bottom") { - popoverStyle = merge(popoverStyle, defaultBottomStyle); + popoverStyle = { ...popoverStyle, ...defaultBottomStyle }; } else if (position === "bottom-left") { - popoverStyle = merge( - popoverStyle, - omit(defaultBottomStyle, ["transform"]), - { - left: rect.left + window.scrollX, - } as React.CSSProperties - ); + popoverStyle = { + ...popoverStyle, + ...twuiOmit(defaultBottomStyle, ["transform"]), + left: rect.left + window.scrollX, + }; } else if (position === "bottom-right") { - popoverStyle = merge( - popoverStyle, - omit(defaultBottomStyle, ["left", "transform"]), - { - right: - viewportWidth - - (rect.left + window.scrollX) - - rect.width - - targetElRightPadding, - } as React.CSSProperties - ); + popoverStyle = { + ...popoverStyle, + ...twuiOmit(defaultBottomStyle, ["left", "transform"]), + right: + viewportWidth - + (rect.left + window.scrollX) - + rect.width - + targetElRightPadding, + }; } else if (position === "top") { - popoverStyle = merge(popoverStyle, defaultTopStyleStyle); + popoverStyle = { ...popoverStyle, ...defaultTopStyleStyle }; } else if (position === "right") { popoverStyle.top = rect.top + window.scrollY + rect.height / 2; popoverStyle.left = rect.right + window.scrollX + 8; diff --git a/src/(partials)/ModalComponent.tsx b/src/(partials)/ModalComponent.tsx index 0440091..6ab4103 100644 --- a/src/(partials)/ModalComponent.tsx +++ b/src/(partials)/ModalComponent.tsx @@ -5,7 +5,7 @@ import Button from "../layout/Button"; import { X } from "lucide-react"; import { type TWUI_MODAL_PROPS } from "../elements/Modal"; import Paper from "../elements/Paper"; -import omit from "lodash/omit"; +import twuiOmit from "../utils/omit"; type Props = TWUI_MODAL_PROPS & { open: boolean; @@ -54,7 +54,7 @@ export default function ModalComponent({ }} > = export default function Form< T extends { [key: string]: any } = { [key: string]: any }, >({ formRef, ...props }: Props) { - const finalProps = omit(props, ["submitHandler", "changeHandler"]); + const finalProps = twuiOmit(props, ["submitHandler", "changeHandler"]); return (
{ + test("leaves out a single key", () => { + expect(twuiOmit({ center: true, gap: 4 }, "center")).toEqual({ gap: 4 }); + }); + + test("leaves out several keys", () => { + const props = { a: 1, b: 2, c: 3 }; + expect(twuiOmit(props, ["a", "c"])).toEqual({ b: 2 }); + }); + + test("does not change the object it was given", () => { + const props = { a: 1, b: 2 }; + twuiOmit(props, "a"); + expect(props).toEqual({ a: 1, b: 2 }); + }); + + test("is shallow: a nested object keeps every key", () => { + const nested = { keep: true, drop: false }; + const result = twuiOmit({ nested, drop: 1 }, "drop"); + expect(result).toEqual({ nested: { keep: true, drop: false } }); + expect(result.nested).toBe(nested); + }); + + test("ignores a named key that is not there", () => { + expect(twuiOmit({ a: 1 }, "missing")).toEqual({ a: 1 }); + }); + + test("returns a new object even when nothing is left out", () => { + const props = { a: 1 }; + expect(twuiOmit(props, [])).toEqual({ a: 1 }); + expect(twuiOmit(props, [])).not.toBe(props); + }); +}); \ No newline at end of file diff --git a/src/utils/omit.ts b/src/utils/omit.ts new file mode 100644 index 0000000..a1b04a5 --- /dev/null +++ b/src/utils/omit.ts @@ -0,0 +1,24 @@ +/** + * A new object without the named keys. Components use it to keep their own + * props off the element they render. + * + * It is shallow, like lodash's omit, but importing lodash for three helpers + * put the whole library in every page that uses a twui component, so this + * lives here instead. + * + * The return type stays T: the keys are runtime-only here, and a spread of + * the result into the element it renders keeps its required props required + * (Partial would make `alt` optional on an img). + */ +export default function twuiOmit( + object: T, + keys: string | string[], +): T { + const result = { ...object } as Record; + + for (const key of Array.isArray(keys) ? keys : [keys]) { + delete result[key]; + } + + return result as T; +} \ No newline at end of file