diff --git a/src/gsap/components/SplitTextWrapper/SplitTextWrapper.tsx b/src/gsap/components/SplitTextWrapper/SplitTextWrapper.tsx index 9e140bd..0b4f924 100644 --- a/src/gsap/components/SplitTextWrapper/SplitTextWrapper.tsx +++ b/src/gsap/components/SplitTextWrapper/SplitTextWrapper.tsx @@ -29,6 +29,10 @@ type SplitTextWrapperProps = { * The element type to render, the default is `div` */ as?: T; + /** + * Split the first element child of the element passed + */ + splitFirstElementChild?: boolean; }; /** @@ -43,7 +47,7 @@ type SplitTextWrapperComponent = ( // @ts-expect-error polymorphic type is not compatible with ensuredForwardRef function factory export const SplitTextWrapper: SplitTextWrapperComponent = ensuredForwardRef( - ({ variables = {}, as, children, ...props }, ref) => { + ({ variables = {}, as, children, splitFirstElementChild = false, ...props }, ref) => { /** * Not using useCallback on purpose so that a new SplitText instance is * created whenever this component rerenders the children @@ -53,7 +57,16 @@ export const SplitTextWrapper: SplitTextWrapperComponent = ensuredForwardRef( return; } - ref.current = new SplitText(element, variables); + if (splitFirstElementChild && element.childElementCount > 1) { + // eslint-disable-next-line no-console + console.warn( + "Split text wrapper should only contain 1 element when 'splitFirstElementChild' is set to true", + ); + } + ref.current = new SplitText( + splitFirstElementChild ? element.firstElementChild : element, + variables, + ); }; const Component = (as ?? 'div') as unknown as ComponentType;