Skip to content

Commit

Permalink
move mt-link to composition api
Browse files Browse the repository at this point in the history
  • Loading branch information
Haberkamp committed Sep 25, 2024
1 parent 6d32afa commit bb5c326
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { within } from "@storybook/test";
import { expect } from "@storybook/test";
import { expect, userEvent, within } from "@storybook/test";

import meta, { type MtLinkMeta, type MtLinkStory } from "./mt-link.stories";

Expand Down Expand Up @@ -27,11 +26,11 @@ export const VisualTestRenderExternalLinkDisabled: MtLinkStory = {
args: {
disabled: true,
},
play: async ({ canvasElement }) => {
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);

const link = canvas.getByText("Link");
await userEvent.click(canvas.getByRole("link"));

expect(getComputedStyle(link).pointerEvents).toEqual("none");
expect(args.click).not.toHaveBeenCalled();
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { fn } from "@storybook/test";
import MtLink from "./mt-link.vue";
import type { SlottedMeta } from "@/_internal/story-helper";

export type MtLinkMeta = SlottedMeta<typeof MtLink, "default" | "close">;
export type MtLinkMeta = SlottedMeta<typeof MtLink, "default" | "close" | "click">;

export default {
title: "Components/Navigation/mt-link",
component: MtLink,
args: {
as: "router-link",
as: "a",
default: "Link",
to: "/",
variant: "primary",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,41 @@
<template>
<component
:is="as"
class="mt-link"
:class="linkClasses"
:class="[
'mt-link',
`mt-link--${variant}`,
{
'mt-link--disabled': disabled,
},
]"
:aria-disabled="disabled"
role="link"
:tabindex="disabled ? -1 : 0"
v-bind="to ? { ...$attrs, to } : $attrs"
@click="onClick"
@click="disabled ? undefined : $emit('click', $event)"
>
<slot />
</component>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import type { PropType } from "vue";
export default defineComponent({
name: "MtLink",
props: {
/**
* Set the element type of the link
*/
as: {
type: String,
required: false,
default: "router-link",
},
/**
* Set the to prop of the router/nuxt-link
*/
to: {
type: String,
required: false,
default: undefined,
},
/**
* Render the link in various styles
*/
variant: {
type: String as PropType<"primary" | "critical">,
required: false,
default: "primary",
validator(value: string) {
if (!value.length) {
return true;
}
return ["primary", "critical"].includes(value);
},
},
/**
* Make the link unclickable
*/
disabled: {
type: Boolean,
required: false,
default: false,
},
},
computed: {
linkClasses() {
return {
[`mt-link--${this.variant}`]: !!this.variant,
"mt-link--disabled": this.disabled,
};
},
<script setup lang="ts">
withDefaults(
defineProps<{
to?: string;
as?: string;
variant?: "primary" | "critical";
disabled?: boolean;
}>(),
{
as: "router-link",
variant: "primary",
disabled: false,
},
);
methods: {
onClick(event: MouseEvent) {
if (this.disabled) {
event.preventDefault();
event.stopImmediatePropagation();
return;
}
this.$emit("click", event);
},
},
});
defineEmits<{
(e: "click", event: MouseEvent): void;
}>();
</script>

<style scoped>
Expand All @@ -99,7 +52,6 @@ export default defineComponent({
.mt-link:is(:disabled, .mt-link--disabled) {
cursor: not-allowed;
pointer-events: none;
}
.mt-link--primary {
Expand Down

0 comments on commit bb5c326

Please sign in to comment.