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

Zeego components not working as expected with tamagui Components #125

Open
ashkalor opened this issue Jan 16, 2025 · 2 comments
Open

Zeego components not working as expected with tamagui Components #125

ashkalor opened this issue Jan 16, 2025 · 2 comments

Comments

@ashkalor
Copy link

I tried to use zeego in a project that had tamagui components

I tried to wrap the custom components like below

import { ComponentProps, FC } from "react";

/**
 * Utilities to make Typescript happy wrapping Zeego components with Tamagui's
 * styled() components. These utils do nothing except assign Typescript types.
 *
 * Makes the following props optional and of `unknown` type:
 * - children
 * - key
 *
 * The fixText() version of the util includes tricks to make Tamagui give the
 * component Text style properties
 */
export type PropsToMakeOptional = "children" | "key";
export type TargetPropsType = {
  [key in PropsToMakeOptional]?: unknown;
};

export const fixView = <T extends {}>(Component: FC<T>) => {
  return Component as T extends TargetPropsType
    ? FC<Omit<T, PropsToMakeOptional> & Partial<Pick<T, PropsToMakeOptional>>>
    : FC<T>;
};

export const fixText = <T extends {}>(Component: FC<T>) => {
  return Component as unknown as T extends TargetPropsType
    ? FC<Omit<T, PropsToMakeOptional> & Partial<Pick<T, PropsToMakeOptional>>> &
        typeof Text
    : FC<T> & typeof Text;
};
import { styled } from "tamagui";
import * as Dropdown from "zeego/dropdown-menu";

const StyledItem = styled(fixView(Dropdown.Item), {
  backgroundColor: "red"
});
export const DropdownMenuItem = Dropdown.create(
  (props: ComponentProps<typeof StyledItem>) => {
    return <StyledItem {...props} />;
  },
  "Item"
);

const StyledItemTitle = styled(
  fixText(Dropdown.ItemTitle),
  {
    color: "blue"
  },
  { isText: true }
);
export const DropdownMenuItemTitle = Dropdown.create(
  (props: ComponentProps<typeof StyledItemTitle>) => {
    return <StyledItemTitle {...props} />;
  },
  "ItemTitle"
);

And tried to use it in a component like,

<DropdownMenu.Root>
      <DropdownMenu.Trigger>
        <Text color="white">Dropdown</Text>
      </DropdownMenu.Trigger>
      <DropdownMenu.Content>
        <DropdownMenuItem key="1">
          <DropdownMenuItemTitle color="blue">Title 1</DropdownMenuItemTitle>
        </DropdownMenuItem>
        <DropdownMenuItem backgroundColor="red" key="2">
          <DropdownMenuItemTitle color="blue">Title 2</DropdownMenuItemTitle>
        </DropdownMenuItem>
      </DropdownMenu.Content>
</DropdownMenu.Root>

No styles are being applied. Any help would be appreciated.

@ashkalor ashkalor changed the title Unable to get it to work with tamagui styling Zeego components not working as expected with tamagui Components Jan 16, 2025
@nandorojo
Copy link
Owner

Zeego v3 will simplify this but it is possible with the current version too, see the custom component docs

@nandorojo
Copy link
Owner

Have to remove styled() from the outside

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants