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

feat(components): Disclosure (fka Accordion) #5873

Merged
merged 8 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ module.exports = {
],
"deprecate/import": [
"error",
{
name: "Accordion",
module: "@arizeai/components",
use: "import { DisclosureGroup, Disclosure, DisclosureTrigger, DisclosurePanel } from '@phoenix/components'",
},
{
name: "Button",
module: "@arizeai/components",
Expand Down
21 changes: 21 additions & 0 deletions app/src/components/StopPropagation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { PropsWithChildren } from "react";

/**
* A component that stops the propagation of events.
*
* This is useful for preventing events from bubbling up to the parent component.
* Such as when buttons are nested, like inside of a DisclosureTrigger.
*/
export function StopPropagation({ children }: PropsWithChildren) {
return (
<div
style={{ display: "contents" }}
onClick={(e) => e.stopPropagation()}
onKeyDown={(e) => e.stopPropagation()}
onMouseDown={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
>
{children}
</div>
);
}
6 changes: 6 additions & 0 deletions app/src/components/button/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ export const buttonCSS = css`
cursor: pointer;
/* Disable outline since there are other mechanisms to show focus */
outline: none;
&[data-focus-visible] {
// Only show outline on focus-visible, aka only when tabbed but not clicked
outline: 1px solid var(--ac-global-input-field-border-color-active);
outline-offset: 1px;
}
&:not([disabled]) {
transition: all 0.2s ease-in-out;
transition: outline 0s;
}
&[disabled] {
cursor: default;
Expand Down
96 changes: 96 additions & 0 deletions app/src/components/disclosure/Disclosure.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { PropsWithChildren } from "react";
import {
Button,
Disclosure as AriaDisclosure,
DisclosureGroup as AriaDisclosureGroup,
type DisclosureGroupProps as AriaDisclosureGroupProps,
DisclosurePanel as AriaDisclosurePanel,
type DisclosurePanelProps as AriaDisclosurePanelProps,
type DisclosureProps as AriaDisclosureProps,
Heading,
} from "react-aria-components";

import { Flex, Icon, Icons } from "@phoenix/components";

import { FlexStyleProps, SizingProps } from "../types";

import { disclosureCSS, disclosureGroupCSS } from "./styles";

export type DisclosureGroupProps = AriaDisclosureGroupProps;

/**
* Wrap multiple Disclosure components in a DisclosureGroup to control
* the expanded state of the items more easily.
*
* AKA Accordion with one or more items
*/
export const DisclosureGroup = (props: DisclosureGroupProps) => {
return (
<AriaDisclosureGroup
allowsMultipleExpanded
css={disclosureGroupCSS}
{...props}
/>
);
};

export type DisclosureProps = AriaDisclosureProps & SizingProps;

/**
* A Disclosure is a component that allows for a single item to be expanded.
*
* AKA Accordion (with a single item) / Accordion Item
*/
export const Disclosure = ({ size, ...props }: DisclosureProps) => {
return (
<AriaDisclosure
css={disclosureCSS}
data-size={size}
defaultExpanded
{...props}
/>
);
};

export type DisclosurePanelProps = AriaDisclosurePanelProps;

/**
* A DisclosurePanel is a component that contains the content of a Disclosure.
*
* AKA Accordion Content
*/
export const DisclosurePanel = (props: DisclosurePanelProps) => {
return <AriaDisclosurePanel {...props} />;
};

export type DisclosureTriggerProps = PropsWithChildren<{
arrowPosition?: "start" | "end";
justifyContent?: FlexStyleProps["justifyContent"];
}>;

/**
* A DisclosureTrigger is a component that triggers the Disclosure.
*
* AKA Accordion Title
*/
export const DisclosureTrigger = ({
children,
arrowPosition,
justifyContent,
}: DisclosureTriggerProps) => {
return (
<Heading>
<Button slot="trigger" data-arrow-position={arrowPosition}>
<Flex
justifyContent={justifyContent}
alignItems="center"
width="100%"
gap="size-100"
>
{children}
</Flex>
<Icon svg={<Icons.ArrowIosForwardOutline />} />
</Button>
</Heading>
);
};
2 changes: 2 additions & 0 deletions app/src/components/disclosure/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Disclosure";
export * from "./styles";
cephalization marked this conversation as resolved.
Show resolved Hide resolved
99 changes: 99 additions & 0 deletions app/src/components/disclosure/styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { css } from "@emotion/react";

export const disclosureGroupCSS = css`
cephalization marked this conversation as resolved.
Show resolved Hide resolved
& > * {
width: 100%;
.react-aria-Heading {
width: 100%;
.react-aria-Button[slot="trigger"] {
width: 100%;
}
}
}

// add border between items, only when child is expanded
> *:not(:last-child) {
&[data-expanded] {
border-bottom: 1px solid var(--ac-global-border-color-default);
}
}
`;

export const disclosureCSS = css`
.react-aria-Heading {
margin: 0;
}

.react-aria-Button[slot="trigger"] {
// reset trigger styles
background: none;
border: none;
box-shadow: none;
font-size: 16px;
font-weight: 400;
line-height: 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: var(--ac-global-dimension-static-size-100)
var(--ac-global-dimension-static-size-200);

// style trigger
color: var(--ac-global-text-color-900);
border-bottom: 1px solid var(--ac-global-border-color-default);
outline: none;
background-color: transparent;
&:hover:not([disabled]) {
background-color: var(--ac-global-input-field-background-color-active);
}
&[data-focus-visible] {
outline: 1px solid var(--ac-global-input-field-border-color-active);
outline-offset: -1px;
}
&:not([disabled]) {
transition: all 0.2s ease-in-out;
}
&[disabled] {
cursor: default;
opacity: 0.6;
}

// style trigger icon
> svg,
> i {
rotate: 90deg;
transition: rotate 200ms;
width: 1em;
height: 1em;
fill: currentColor;
}

&[data-arrow-position="start"] {
flex-direction: row-reverse;
> svg,
> i {
rotate: 0deg;
}
}
}

&[data-size="L"] .react-aria-Button[slot="trigger"] {
height: 48px;
max-height: 48px;
}

&[data-expanded] .react-aria-Button[slot="trigger"] {
> svg,
> i {
rotate: -90deg;
}

&[data-arrow-position="start"] {
> svg,
> i {
rotate: 90deg;
}
}
}
`;
1 change: 1 addition & 0 deletions app/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from "./ViewSummaryAside";
export * from "./CopyToClipboardButton";

// design system based components
export * from "./disclosure";
export * from "./combobox";
export * from "./button";
export * from "./icon";
Expand Down
Loading
Loading