From 306791c85047476db296445d01af1237c46416cf Mon Sep 17 00:00:00 2001 From: Arno V Date: Thu, 26 Sep 2024 16:04:34 -0400 Subject: [PATCH 1/4] fix(TextInput): misaligned label and helper text at different sizes --- .../src/Form/TextInput.stories.tsx | 26 ++++++++ .../tailwindcss/components/textInput.ts | 2 +- .../src/components/TextInput/TextInput.tsx | 63 ++++++++++++++++++- .../TextInput/__tests__/TextInput.test.tsx | 4 +- .../src/components/TextInput/utilities.ts | 16 ++--- 5 files changed, 99 insertions(+), 12 deletions(-) diff --git a/packages/documentation/src/Form/TextInput.stories.tsx b/packages/documentation/src/Form/TextInput.stories.tsx index b6620bc8..0f395d31 100644 --- a/packages/documentation/src/Form/TextInput.stories.tsx +++ b/packages/documentation/src/Form/TextInput.stories.tsx @@ -65,3 +65,29 @@ RightElement.args = { ), helperText: "Powered by the sun", }; + +export const AllSizes: Story = (args) => ( +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+); +AllSizes.args = { + helperText: "Powered by the sun", + label: "Pose your question here", +}; diff --git a/packages/ui-styles/src/plugins/tailwindcss/components/textInput.ts b/packages/ui-styles/src/plugins/tailwindcss/components/textInput.ts index bde7a9df..7209ebf8 100644 --- a/packages/ui-styles/src/plugins/tailwindcss/components/textInput.ts +++ b/packages/ui-styles/src/plugins/tailwindcss/components/textInput.ts @@ -8,7 +8,7 @@ export default { /* move the label above the field (on focus) */ [`.av-text-input:focus + label[aria-hidden="true"], .av-text-input:not(:placeholder-shown) + label[aria-hidden="true"]`]: { - transform: "translate(12px, var(--av-text-input-label, -37px)) scale(0.75)", + transform: "translate(12px, var(--av-text-input-label, -33px)) scale(0.75)", }, /* move the helper text below the field */ ".av-text-input-helper-text": { diff --git a/packages/ui-textinput/src/components/TextInput/TextInput.tsx b/packages/ui-textinput/src/components/TextInput/TextInput.tsx index 43b6a66c..5b1554f4 100644 --- a/packages/ui-textinput/src/components/TextInput/TextInput.tsx +++ b/packages/ui-textinput/src/components/TextInput/TextInput.tsx @@ -1,6 +1,6 @@ import { useResizeObserver, useUniqueId } from "@versini/ui-hooks"; import { LiveRegion } from "@versini/ui-private"; -import React, { useLayoutEffect, useState } from "react"; +import React, { useLayoutEffect, useRef, useState } from "react"; import { TEXT_INPUT_CLASSNAME } from "../../common/constants"; import type { TextInputProps } from "./TextInputTypes"; @@ -40,6 +40,9 @@ export const TextInput = React.forwardRef( const [inputPaddingRight, setInputPaddingRight] = useState(0); const inputId = useUniqueId({ id, prefix: `${TEXT_INPUT_CLASSNAME}-` }); const liveErrorMessage = `${name} error, ${helperText}`; + const labelRef = useRef(null); + const helperTextRef = useRef(null); + const textInputClassName = getTextInputClasses({ className, inputClassName, @@ -68,6 +71,62 @@ export const TextInput = React.forwardRef( }, [rect]); /* c8 ignore end */ + useLayoutEffect(() => { + switch (size) { + case "xs": + labelRef?.current?.style.setProperty( + "--av-text-input-label", + "-25px", + ); + helperTextRef?.current?.style.setProperty( + "--av-text-input-helper-text", + "29px", + ); + break; + case "sm": + labelRef?.current?.style.setProperty( + "--av-text-input-label", + "-29px", + ); + helperTextRef?.current?.style.setProperty( + "--av-text-input-helper-text", + "33px", + ); + break; + case "lg": + labelRef?.current?.style.setProperty( + "--av-text-input-label", + "-15px", + ); + helperTextRef?.current?.style.setProperty( + "--av-text-input-helper-text", + "22px", + ); + break; + case "xl": + labelRef?.current?.style.setProperty( + "--av-text-input-label", + "-19px", + ); + helperTextRef?.current?.style.setProperty( + "--av-text-input-helper-text", + "25px", + ); + break; + + default: + labelRef?.current?.style.setProperty( + "--av-text-input-label", + "-33px", + ); + helperTextRef?.current?.style.setProperty( + "--av-text-input-helper-text", + "37px", + ); + break; + } + }, [size]); + return (