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

[Feature]: Add ToggleGroup #104

Closed
2 tasks
maelp opened this issue Oct 6, 2023 · 5 comments
Closed
2 tasks

[Feature]: Add ToggleGroup #104

maelp opened this issue Oct 6, 2023 · 5 comments

Comments

@maelp
Copy link

maelp commented Oct 6, 2023

Describe the feature

I'd like to use the radix-vue ToggleGroup but it isn't yet into shadcn (although "Toggle" is).

I tried looking at the radix-vue code but it seems that ToggleGroupItem already includes the radix-vue "Toggle" item, so I guess it wouldn't use the shadcn "Toggle" item, in this case does this mean we have to reimplement it completely?

Would you show me how to help reimplement it?

Additional information

  • I intend to submit a PR for this feature.
  • I have already implemented and/or tested this feature.
@maelp
Copy link
Author

maelp commented Oct 6, 2023

(I'm not sure how complex the feature would be to implement? is it just re-using radix-vue? do we need to copy-paste the whole code and adapt? or is there another way?)

@zernonia
Copy link
Member

zernonia commented Oct 7, 2023

Yeah we can add that. And I think we can adapt the current Toggle's style here as well

@maelp
Copy link
Author

maelp commented Oct 7, 2023

Nice, that's how I did it for now (I'm using the regular radix ToggleRoot and adapting the item that way)

<script setup lang="ts">
import { withDefaults, defineProps } from "vue";
import type { ToggleGroupItemProps } from "radix-vue";
import { ToggleGroupItem } from "radix-vue";
import type { VariantProps } from "class-variance-authority";
import { computed } from "vue";
import { toggleGroupItemVariants } from ".";
import { cn, useEmitAsProps } from "../frameworkUtils";
import { GIcon } from "@/ui";

type ToggleGroupItemVariantProps = VariantProps<typeof toggleGroupItemVariants>;

interface Props extends ToggleGroupItemProps {
  variant?: ToggleGroupItemVariantProps["variant"];
  size?: ToggleGroupItemVariantProps["size"];
  icon?: string;
  title?: string;
}
const props = withDefaults(defineProps<Props>(), {
  variant: "default",
  size: "default",
});

const toggleProps = computed(() => {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { variant, size, ...otherProps } = props;
  return otherProps;
});
</script>

<template>
  <ToggleGroupItem
    v-bind="{ ...toggleProps }"
    :class="
      cn(toggleGroupItemVariants({ variant, size, class: $attrs.class ?? '' }))
    "
  >
    <GIcon
      :icon="props.icon"
      class="h-[15px] w-[15px] mr-1"
      v-if="props.icon"
    ></GIcon>
    <span class="text-sm" v-if="props.title">{{ props.title }}</span>
    <slot />
  </ToggleGroupItem>
</template>

and

import { cva } from "class-variance-authority";

export { ToggleGroupRoot } from "radix-vue";
export { default as ToggleGroupItem } from "./ToggleGroupItem.vue";

export const toggleGroupItemVariants = cva(
  "inline-flex items-center justify-center text-sm font-medium ring-offset-background transition-colors hover:bg-muted hover:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground first:rounded-l-md last:rounded-r-md",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground hover:bg-primary/90",
        outline:
          "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
      },
      size: {
        default: "h-10 px-3",
        sm: "h-8 px-2",
        lg: "h-11 px-5",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
);

but there's probably a cleaner way to do it :)

@Tajcore
Copy link

Tajcore commented Jan 14, 2024

Question how did you use this within a form

@sadeghbarati
Copy link
Collaborator

#275

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

4 participants