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

Simplify IconButton to not use react-aria #683

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 8 additions & 27 deletions packages/graph-explorer/src/components/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { cn } from "@/utils";
import { useButton } from "@react-aria/button";
import type { AriaButtonProps } from "@react-types/button";
import type { ElementType, ForwardedRef, ReactNode, RefObject } from "react";
import type { ComponentPropsWithoutRef, ForwardedRef, ReactNode } from "react";
import { forwardRef } from "react";
import type { TooltipProps } from "./Tooltip";
import Tooltip from "./Tooltip/Tooltip";
Expand Down Expand Up @@ -53,54 +51,37 @@ const iconButtonVariants = cva({
},
});

type IconButtonVariants = VariantProps<typeof iconButtonVariants>;

export interface IconButtonProps
extends Omit<AriaButtonProps<ElementType<any>>, "elementType">,
IconButtonVariants {
className?: string;
extends Omit<ComponentPropsWithoutRef<"button">, "color">,
VariantProps<typeof iconButtonVariants> {
icon: ReactNode;
isDisabled?: boolean;
as?: ElementType;
tooltipText?: TooltipProps["text"];
tooltipPlacement?: TooltipProps["placement"];
onClick?(e: MouseEvent): void;
}

export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
(
{
tooltipText,
tooltipPlacement,
onClick,
variant,
size,
color,
className,
icon,
...props
}: IconButtonProps,
ref: ForwardedRef<HTMLButtonElement>
) => {
const { buttonProps } = useButton(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
props,
ref as RefObject<HTMLButtonElement>
);

const { className, icon, as } = props;

const Component: ElementType = as ? as : "button";

const component = (
<Component
<button
ref={ref}
{...buttonProps}
className={cn(iconButtonVariants({ variant, color, size }), className)}
onClick={onClick}
{...props}
>
{icon}
{props.children}
</Component>
</button>
);

if (tooltipText) {
Expand Down
6 changes: 3 additions & 3 deletions packages/graph-explorer/src/components/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function PanelHeaderCloseButton({
<IconButton
tooltipText="Close"
icon={<XIcon />}
onPress={onClose}
onClick={onClose}
variant="text"
size="small"
/>
Expand All @@ -140,12 +140,12 @@ export function PanelHeaderActionButton({
}: Action & IconButtonProps) {
return (
<IconButton
isDisabled={isDisabled}
disabled={isDisabled}
tooltipText={label}
variant={active ? "filled" : "text"}
color={color}
icon={icon}
onPress={() => onActionClick()}
onClick={() => onActionClick()}
{...props}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ const SelectBox = (
{clearable && selectedOptions !== "" && (
<IconButton
className={"clear-button"}
as="span"
variant="text"
icon={<CloseIcon />}
onPress={() => onSelectionChange?.(new Set())}
onClick={() => onSelectionChange?.(new Set())}
/>
)}
{items.length > 0 && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const makeIconActionCell =
icon={getIcon?.(props) || icon}
size={"small"}
variant={"text"}
onPress={() => onPress(props)}
onClick={() => onPress(props)}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const makeIconActionCell =
icon={getValue(props) ? on : off}
size={"small"}
variant={"text"}
onPress={() => onPress?.(props)}
onClick={() => onPress?.(props)}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const ColumnSettingsControl = <T extends Record<string, unknown>>({
variant={"text"}
size={"base"}
icon={<ManageColumnsIcon />}
onPress={() => setIsContentVisible(visible => !visible)}
onClick={() => setIsContentVisible(visible => !visible)}
{...triggerProps}
/>
{renderLayer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,20 @@ export const PaginationControl: FunctionComponent<PaginationControlProps> = ({
onChange={value => onPageSizeChange(parseInt(value as string))}
/>
<IconButton
isDisabled={pageIndex - 1 < 0}
disabled={pageIndex - 1 < 0}
variant={"text"}
size={"small"}
className={cn("page-button", "page-control")}
icon={<SkipBackwardIcon />}
onPress={() => onPageIndexChange(0)}
onClick={() => onPageIndexChange(0)}
/>
<IconButton
isDisabled={pageIndex - 1 < 0}
disabled={pageIndex - 1 < 0}
variant={"text"}
size={"small"}
className={cn("page-button", "page-control")}
icon={<BackwardIcon />}
onPress={() => onPageIndexChange(pageIndex - 1)}
onClick={() => onPageIndexChange(pageIndex - 1)}
/>
<div className={"page-viz"}>
{pagesToRender.map(page => {
Expand All @@ -196,19 +196,19 @@ export const PaginationControl: FunctionComponent<PaginationControlProps> = ({
})}
</div>
<IconButton
isDisabled={pageIndex + 1 >= pageCount}
disabled={pageIndex + 1 >= pageCount}
variant={"text"}
size={"small"}
className={cn("page-button", "page-control")}
icon={<ForwardIcon />}
onPress={() => onPageIndexChange(pageIndex + 1)}
onClick={() => onPageIndexChange(pageIndex + 1)}
/>
<IconButton
isDisabled={pageIndex + 1 >= pageCount}
disabled={pageIndex + 1 >= pageCount}
variant={"text"}
className={cn("page-button", "page-control")}
icon={<SkipForwardIcon />}
onPress={() => onPageIndexChange(pageCount - 1)}
onClick={() => onPageIndexChange(pageCount - 1)}
/>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,20 @@ export const PaginationControl: FunctionComponent<PaginationControlProps> = ({
onChange={value => setPageSize(parseInt(value as string))}
/>
<IconButton
isDisabled={!canPreviousPage}
disabled={!canPreviousPage}
variant={"text"}
size={"small"}
className={cn("page-button", "page-control")}
icon={<SkipBackwardIcon />}
onPress={() => gotoPage(0)}
onClick={() => gotoPage(0)}
/>
<IconButton
isDisabled={!canPreviousPage}
disabled={!canPreviousPage}
variant={"text"}
size={"small"}
className={cn("page-button", "page-control")}
icon={<BackwardIcon />}
onPress={previousPage}
onClick={previousPage}
/>
<div className={"page-viz"}>
{pagesToRender.map(page => {
Expand All @@ -203,19 +203,19 @@ export const PaginationControl: FunctionComponent<PaginationControlProps> = ({
})}
</div>
<IconButton
isDisabled={!canNextPage}
disabled={!canNextPage}
variant={"text"}
size={"small"}
className={cn("page-button", "page-control")}
icon={<ForwardIcon />}
onPress={nextPage}
onClick={nextPage}
/>
<IconButton
isDisabled={!canNextPage}
disabled={!canNextPage}
variant={"text"}
className={cn("page-button", "page-control")}
icon={<SkipForwardIcon />}
onPress={() => gotoPage(pageCount - 1)}
onClick={() => gotoPage(pageCount - 1)}
/>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const ConnectionData = () => {
icon={<ChevronRightIcon />}
variant="text"
size="small"
onPress={() => navigate(`/data-explorer/${encodeURIComponent(vt)}`)}
onClick={() => navigate(`/data-explorer/${encodeURIComponent(vt)}`)}
/>
),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default function GraphViewer({
tooltipPlacement="bottom-center"
icon={<ResetIcon />}
variant="text"
onPress={() => {
onClick={() => {
graphRef.current?.runLayout();
}}
/>
Expand Down Expand Up @@ -302,7 +302,7 @@ function Legend({ onClose }: { onClose: () => void }) {
<h1 className="text-base font-bold">Legend</h1>
<IconButton
icon={<CloseIcon />}
onPress={onClose}
onClick={onClose}
variant="text"
size="small"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const UserPrefixes = () => {
size={"small"}
color={"error"}
icon={<DeleteIcon />}
onPress={onDeletePrefix(prefixConfig.prefix)}
onClick={onDeletePrefix(prefixConfig.prefix)}
/>
),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const NodeExpandFilters = ({
icon={<AddIcon />}
variant={"text"}
size={"small"}
onPress={onFilterAdd}
onClick={onFilterAdd}
/>
</div>
)}
Expand Down Expand Up @@ -127,7 +127,7 @@ const NodeExpandFilters = ({
variant="text"
color="error"
tooltipText="Remove Filter"
onPress={() => onFilterDelete(filterIndex)}
onClick={() => onFilterDelete(filterIndex)}
/>
</div>
))}
Expand All @@ -145,7 +145,7 @@ const NodeExpandFilters = ({
icon={<AddIcon />}
variant="text"
size="small"
onPress={() => onLimitChange(1)}
onClick={() => onLimitChange(1)}
/>
</div>
{limit !== null && (
Expand All @@ -166,7 +166,7 @@ const NodeExpandFilters = ({
variant="text"
color="error"
tooltipText="Remove Limit"
onPress={() => onLimitChange(null)}
onClick={() => onLimitChange(null)}
/>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const GraphExplorer = () => {
tooltipPlacement={"bottom-center"}
variant={toggles.has("graph-viewer") ? "filled" : "text"}
icon={<GraphIcon />}
onPress={toggleView("graph-viewer")}
onClick={toggleView("graph-viewer")}
/>
<IconButton
tooltipText={
Expand All @@ -157,7 +157,7 @@ const GraphExplorer = () => {
tooltipPlacement={"bottom-center"}
variant={toggles.has("table-view") ? "filled" : "text"}
icon={<GridIcon />}
onPress={toggleView("table-view")}
onClick={toggleView("table-view")}
/>
<div className={"v-divider"} />
<Link to={"/connections"}>
Expand Down
Loading