Skip to content

Commit

Permalink
Re-add the option to split first element child on the SplitTextWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
tworrisb committed Dec 12, 2023
1 parent d45f83a commit 154eb91
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/gsap/components/SplitTextWrapper/SplitTextWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type SplitTextWrapperProps<T extends KnownTarget> = {
* The element type to render, the default is `div`
*/
as?: T;
/**
* Split the first element child of the element passed
*/
splitFirstElementChild?: boolean;
};

/**
Expand All @@ -43,7 +47,7 @@ type SplitTextWrapperComponent = <T extends KnownTarget = 'div'>(

// @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
Expand All @@ -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<unknown>;
Expand Down

0 comments on commit 154eb91

Please sign in to comment.