Skip to content

Commit

Permalink
chore: migrate vui-source changes to v1.0.2 (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrderyk authored Dec 6, 2024
1 parent 6df318d commit 2a7f6c3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vectara/vectara-ui",
"version": "1.0.1",
"version": "1.0.2",
"homepage": "https://vectara.github.io/vectara-ui/",
"description": "Vectara's design system, codified as a React and Sass component library",
"author": "Vectara",
Expand Down
87 changes: 57 additions & 30 deletions src/docs/pages/popover/Popover.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { useState } from "react";
import { VuiButtonSecondary, VuiIcon, VuiOptionsList, VuiPopover } from "../../../lib";
import {
AnchorSide,
VuiButtonSecondary,
VuiFormGroup,
VuiIcon,
VuiOptionsList,
VuiPopover,
VuiSelect,
VuiSpacer
} from "../../../lib";
import { BiCaretDown } from "react-icons/bi";

const options = [
Expand All @@ -11,36 +20,54 @@ const options = [
export const Popover = () => {
const [isOpen, setIsOpen] = useState(false);
const [selectedOption, setSelectedOption] = useState("apples");
const [anchorSide, setAnchorSide] = useState<AnchorSide>("right");

return (
<VuiPopover
isOpen={isOpen}
setIsOpen={() => setIsOpen(!isOpen)}
header="Tribes"
button={
<VuiButtonSecondary
color="neutral"
size="s"
icon={
<VuiIcon size="m">
<BiCaretDown />
</VuiIcon>
}
>
Tribe: {selectedOption}
</VuiButtonSecondary>
}
>
<VuiOptionsList
isSelectable
isScrollable
onSelectOption={(value: string) => {
setIsOpen(false);
setSelectedOption(value);
}}
selected={selectedOption}
options={options}
/>
</VuiPopover>
<>
<VuiFormGroup labelFor="anchorSideSelect" label="Anchor side">
<VuiSelect
id="anchorSideSelect"
options={[
{ text: "Left", value: "left" },
{ text: "Right", value: "right" }
]}
value={anchorSide}
onChange={(event) => setAnchorSide(event.target.value as AnchorSide)}
/>
</VuiFormGroup>

<VuiSpacer size="m" />

<VuiPopover
anchorSide={anchorSide}
isOpen={isOpen}
setIsOpen={() => setIsOpen(!isOpen)}
header="Tribes"
button={
<VuiButtonSecondary
color="neutral"
size="s"
icon={
<VuiIcon size="m">
<BiCaretDown />
</VuiIcon>
}
>
Tribe: {selectedOption}
</VuiButtonSecondary>
}
>
<VuiOptionsList
isSelectable
isScrollable
onSelectOption={(value: string) => {
setIsOpen(false);
setSelectedOption(value);
}}
selected={selectedOption}
options={options}
/>
</VuiPopover>
</>
);
};
3 changes: 2 additions & 1 deletion src/lib/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { VuiOptionsButton } from "./optionsButton/OptionsButton";
import { VuiOptionsList } from "./optionsList/OptionsList";
import { VuiOptionsListItem } from "./optionsList/OptionsListItem";
import { OptionListItem } from "./optionsList/types";
import { VuiPopover } from "./popover/Popover";
import { VuiPopover, AnchorSide } from "./popover/Popover";
import { VuiPortal } from "./portal/Portal";
import { PROGRESS_BAR_COLOR, VuiProgressBar } from "./progressBar/ProgressBar";
import { VuiPrompt } from "./prompt/Prompt";
Expand Down Expand Up @@ -84,6 +84,7 @@ import { VuiToggle } from "./toggle/Toggle";
import { VuiTopicButton } from "./topicButton/TopicButton";

export type {
AnchorSide,
AppContentPadding,
ButtonColor,
CalloutColor,
Expand Down
31 changes: 21 additions & 10 deletions src/lib/components/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import classNames from "classnames";
import { VuiPortal } from "../portal/Portal";
import { FocusOn } from "react-focus-on";

export type AnchorSide = "left" | "right";

export type Props = {
button: React.ReactElement;
children?: React.ReactNode;
Expand All @@ -11,20 +13,28 @@ export type Props = {
isOpen: boolean;
setIsOpen: (isOpen: boolean) => void;
padding?: boolean;
anchorSide?: AnchorSide;
};

type Position = {
top: number;
right: number;
top: string;
left?: string;
right?: string;
};

const getPosition = (button: HTMLElement | null): Position | undefined => {
const calculatePopoverPosition = (button: HTMLElement | null, anchorSide: AnchorSide): Position | undefined => {
if (!button) return undefined;
const { bottom, right } = button.getBoundingClientRect();
return {
top: bottom + 2 + document.documentElement.scrollTop,
right: window.innerWidth - right
};

const buttonRect = button.getBoundingClientRect();
const top = buttonRect.bottom + 2 + document.documentElement.scrollTop;
const left = buttonRect.left;

if (anchorSide === "left") {
return { top: `${top}px`, left: `${left}px` };
}

const right = window.innerWidth - buttonRect.right;
return { top: `${top}px`, right: `${right}px` };
};

export const VuiPopover = ({
Expand All @@ -35,6 +45,7 @@ export const VuiPopover = ({
isOpen,
setIsOpen,
padding,
anchorSide = "right",
...rest
}: Props) => {
const returnFocusElRef = useRef<HTMLElement | null>(null);
Expand Down Expand Up @@ -90,7 +101,7 @@ export const VuiPopover = ({
// Always keep menu position up to date. If we tried to cache this inside
// a useEffect based on isOpen then there'd be a flicker if the width
// of the button changes.
const position = getPosition(buttonRef.current);
const position = calculatePopoverPosition(buttonRef.current, anchorSide);

const classes = classNames("vuiPopover", className);

Expand All @@ -117,7 +128,7 @@ export const VuiPopover = ({
// Enable scrolling of the page.
preventScrollOnFocus={false}
>
<div className={classes} style={{ top: `${position.top}px`, right: `${position.right}px` }} {...rest}>
<div className={classes} style={position} {...rest}>
{header && typeof header === "string" ? <div className="vuiPopoverTitle">{header}</div> : header}
{children && <div className={contentClasses}>{children}</div>}
</div>
Expand Down

0 comments on commit 2a7f6c3

Please sign in to comment.