Drop lodash from the components, so no page carries the library
This commit is contained in:
+1
-1
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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({
|
||||
}}
|
||||
></div>
|
||||
<Paper
|
||||
{...omit(props, [
|
||||
{...twuiOmit(props, [
|
||||
"target",
|
||||
"targetRef",
|
||||
"targetWrapperProps",
|
||||
|
||||
@@ -3,7 +3,7 @@ import { twMerge } from "tailwind-merge";
|
||||
import ReactDOM from "react-dom";
|
||||
import type { TWUI_MODAL_PROPS } from "../elements/Modal";
|
||||
import Paper from "../elements/Paper";
|
||||
import omit from "lodash/omit";
|
||||
import twuiOmit from "../utils/omit";
|
||||
import twuiGrabPopoverStyles from "../(functions)/popver/grab-popover-styles";
|
||||
|
||||
type Props = TWUI_MODAL_PROPS & {
|
||||
@@ -93,7 +93,7 @@ export default function PopoverComponent({
|
||||
|
||||
return ReactDOM.createPortal(
|
||||
<Paper
|
||||
{...omit(props, [
|
||||
{...twuiOmit(props, [
|
||||
"target",
|
||||
"targetRef",
|
||||
"targetWrapperProps",
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import omit from "lodash/omit";
|
||||
import twuiOmit from "../utils/omit";
|
||||
import type { DetailedHTMLProps, FormHTMLAttributes, RefObject } from "react";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
@@ -16,7 +16,7 @@ type Props<T extends { [key: string]: any } = { [key: string]: any }> =
|
||||
export default function Form<
|
||||
T extends { [key: string]: any } = { [key: string]: any },
|
||||
>({ formRef, ...props }: Props<T>) {
|
||||
const finalProps = omit(props, ["submitHandler", "changeHandler"]);
|
||||
const finalProps = twuiOmit(props, ["submitHandler", "changeHandler"]);
|
||||
|
||||
return (
|
||||
<form
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import omit from "lodash/omit";
|
||||
import twuiOmit from "../utils/omit";
|
||||
import React, {
|
||||
type DetailedHTMLProps,
|
||||
type ImgHTMLAttributes,
|
||||
@@ -44,7 +44,7 @@ export default function Img({
|
||||
|
||||
const [imageError, setImageError] = React.useState(false);
|
||||
|
||||
const finalProps = omit(props, [
|
||||
const finalProps = twuiOmit(props, [
|
||||
"size",
|
||||
"circle",
|
||||
"bgImg",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import omit from "lodash/omit";
|
||||
import twuiOmit from "../utils/omit";
|
||||
import type { DetailedHTMLProps, HTMLAttributes } from "react";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
@@ -16,7 +16,7 @@ type Props = DetailedHTMLProps<
|
||||
* @className twui-stack
|
||||
*/
|
||||
export default function Stack({ gap, componentRef, ...props }: Props) {
|
||||
const finalProps = omit(props, "center");
|
||||
const finalProps = twuiOmit(props, "center");
|
||||
return (
|
||||
<div
|
||||
{...finalProps}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import twuiOmit from "./omit";
|
||||
|
||||
describe("twuiOmit", () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -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<T> would make `alt` optional on an img).
|
||||
*/
|
||||
export default function twuiOmit<T extends object>(
|
||||
object: T,
|
||||
keys: string | string[],
|
||||
): T {
|
||||
const result = { ...object } as Record<string, unknown>;
|
||||
|
||||
for (const key of Array.isArray(keys) ? keys : [keys]) {
|
||||
delete result[key];
|
||||
}
|
||||
|
||||
return result as T;
|
||||
}
|
||||
Reference in New Issue
Block a user