Skip to content

Commit

Permalink
Pill add truncation story (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fercas123 authored Nov 13, 2023
1 parent d06066d commit 1ea2d86
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions packages/lab/stories/pill-next/pill-next.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PillNext } from "@salt-ds/lab";
import { FavoriteIcon } from "@salt-ds/icons";
import { Meta, StoryFn } from "@storybook/react";
import { useState } from "react";
import { ChangeEvent, Ref, useEffect, useRef, useState } from "react";
import { shortColorData } from "./../assets/exampleData";
import { Button, FlowLayout } from "@salt-ds/core";
import { Button, FlowLayout, Input, StackLayout, Tooltip } from "@salt-ds/core";

export default {
title: "Lab/Pill Next",
Expand Down Expand Up @@ -65,3 +65,37 @@ export const Icon: StoryFn<typeof PillNext> = () => {
</PillNext>
);
};

export const Truncated: StoryFn<typeof PillNext> = () => {
const pillRef = useRef<HTMLButtonElement | null>(null);
const [maxWidth, setMaxWidth] = useState<string>("150");
const [isEllipsisActive, setEllipsisActive] = useState(false);

useEffect(() => {
const text = pillRef?.current?.firstElementChild as HTMLElement;
setEllipsisActive(text?.offsetWidth < text?.scrollWidth);
}, [maxWidth]);

const content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

return (
<StackLayout direction={"column"}>
<Input
inputProps={{ type: "number", step: 50 }}
style={{ maxWidth: "150px" }}
defaultValue={maxWidth}
onChange={(event: ChangeEvent<HTMLInputElement>) =>
setMaxWidth(event.target.value)
}
/>
<Tooltip content={content} disabled={!isEllipsisActive}>
<PillNext
ref={pillRef as Ref<HTMLButtonElement>}
style={{ maxWidth: `${maxWidth}px` }}
>
{content}
</PillNext>
</Tooltip>
</StackLayout>
);
};

1 comment on commit 1ea2d86

@vercel
Copy link

@vercel vercel bot commented on 1ea2d86 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.