From c65cd3eb099e8e4babe1b424a88daf017f3782ed Mon Sep 17 00:00:00 2001 From: Julio Salas <84051776+julio-salas03@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:22:32 -0500 Subject: [PATCH] update tag prop tying to exclude svg elements --- docs/src/content/docs/api.mdx | 2 +- src/components/SplitText.tsx | 10 +++++----- src/utils.ts | 11 +++++++++++ tests/index.test.tsx | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/src/content/docs/api.mdx b/docs/src/content/docs/api.mdx index 9a504de..f6234a2 100644 --- a/docs/src/content/docs/api.mdx +++ b/docs/src/content/docs/api.mdx @@ -26,7 +26,7 @@ Wrap your text with this component. The text will automatically be wrapped, by d | **extraProps** | An extra value that will be forwarded to each wrapper. | `T` | | **children** | The content to be split and rendered. | `ReactNode` | | **debounceTime** | The time the resize listener should be [debounced](https://medium.com/@jamischarles/what-is-debouncing-2505c0648ff1) by. In milliseconds. | `number` | -| **tag** | The HTML tag as the container for the text. | `JSX.ElementType` | +| **tag** | The HTML tag as the container for the text. | `HTMLElementTag` | ### Example diff --git a/src/components/SplitText.tsx b/src/components/SplitText.tsx index 211ae6a..92760c4 100644 --- a/src/components/SplitText.tsx +++ b/src/components/SplitText.tsx @@ -7,7 +7,7 @@ import { WordWrapperProps, LineWrapperProps, } from './Wrappers'; -import { debounce } from '../utils'; +import { debounce, HTMLElementTag } from '../utils'; const DefaultLineWrapper = React.memo(LineWrapper); const DefaultWordWrapper = React.memo(WordWrapper); @@ -62,14 +62,14 @@ export interface SplitTextProps { /** * The HTML tag as the container for the text. * @default "span" - * @type JSX.ElementType + * @type HTMLElementTag */ - tag?: JSX.ElementType; + tag?: HTMLElementTag; } const DEFAULT_CONTAINER_TAG = 'span'; -export const SplitText = React.forwardRef( +export const SplitText = React.forwardRef( function SplitText( { children, @@ -86,7 +86,7 @@ export const SplitText = React.forwardRef( ) { let text = ''; - const Container = tag || DEFAULT_CONTAINER_TAG; + const Container = (tag || DEFAULT_CONTAINER_TAG) as JSX.ElementType; React.Children.map(children, child => { if (typeof child === 'string' || typeof child === 'number') { diff --git a/src/utils.ts b/src/utils.ts index 6dafec4..bd095b0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import React from 'react'; + export function debounce( func: (...args: T) => any, timeout: number @@ -10,3 +12,12 @@ export function debounce( }, timeout); }; } + +type KeysMatching = { + [K in keyof T]-?: T[K] extends V ? K : never; +}[keyof T]; + +export type HTMLElementTag = KeysMatching< + JSX.IntrinsicElements, + React.DetailedHTMLProps, HTMLElement> +>; diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 0dbd930..0304bad 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -49,7 +49,7 @@ describe('SplitText', () => { let checkStyles = () => {}; const Component = () => { - const ref = React.useRef(null); + const ref = React.useRef(null); checkStyles = () => { const el = ref.current; @@ -72,7 +72,7 @@ describe('SplitText', () => { let checkStyles = () => {}; const Component = () => { - const ref = React.useRef(null); + const ref = React.useRef(null); checkStyles = () => { const el = ref.current;