Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add the option to split first element child on the SplitTextWrapper #294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading