diff --git a/app/components/ActionButton.tsx b/app/components/ActionButton.tsx new file mode 100644 index 0000000000..4472a0e6d7 --- /dev/null +++ b/app/components/ActionButton.tsx @@ -0,0 +1,73 @@ +import * as React from "react"; +import Tooltip, { Props as TooltipProps } from "~/components/Tooltip"; +import { Action, ActionContext } from "~/types"; + +export type Props = { + /** Show the button in a disabled state */ + disabled?: boolean; + /** Hide the button entirely if action is not applicable */ + hideOnActionDisabled?: boolean; + /** Action to use on button */ + action?: Action; + /** Context of action, must be provided with action */ + context?: ActionContext; + /** If tooltip props are provided the button will be wrapped in a tooltip */ + tooltip?: Omit; +}; + +/** + * Button that can be used to trigger an action definition. + */ +const ActionButton = React.forwardRef( + ( + { + action, + context, + tooltip, + hideOnActionDisabled, + ...rest + }: Props & React.HTMLAttributes, + ref: React.Ref + ) => { + const disabled = rest.disabled; + + if (!context || !action) { + return + ); + + if (tooltip) { + return {button}; + } + + return button; + } +); + +export default ActionButton; diff --git a/app/components/NudeButton.tsx b/app/components/NudeButton.tsx index 558428669a..8412c2bf52 100644 --- a/app/components/NudeButton.tsx +++ b/app/components/NudeButton.tsx @@ -1,12 +1,18 @@ import styled from "styled-components"; +import ActionButton, { + Props as ActionButtonProps, +} from "~/components/ActionButton"; -const Button = styled.button.attrs((props) => ({ - type: "type" in props ? props.type : "button", -}))<{ +type Props = ActionButtonProps & { width?: number; height?: number; size?: number; -}>` + type?: "button" | "submit" | "reset"; +}; + +const StyledNudeButton = styled(ActionButton).attrs((props: Props) => ({ + type: "type" in props ? props.type : "button", +}))` width: ${(props) => props.width || props.size || 24}px; height: ${(props) => props.height || props.size || 24}px; background: none; @@ -20,4 +26,4 @@ const Button = styled.button.attrs((props) => ({ color: inherit; `; -export default Button; +export default StyledNudeButton; diff --git a/app/components/Sidebar/components/CollectionLink.tsx b/app/components/Sidebar/components/CollectionLink.tsx index da094d60da..6484b94105 100644 --- a/app/components/Sidebar/components/CollectionLink.tsx +++ b/app/components/Sidebar/components/CollectionLink.tsx @@ -4,7 +4,7 @@ import { PlusIcon } from "outline-icons"; import * as React from "react"; import { useDrop, useDrag, DropTargetMonitor } from "react-dnd"; import { useTranslation } from "react-i18next"; -import { useLocation, useHistory, Link } from "react-router-dom"; +import { useLocation, useHistory } from "react-router-dom"; import styled from "styled-components"; import { sortNavigationNodes } from "@shared/utils/collections"; import Collection from "~/models/Collection"; @@ -14,13 +14,13 @@ import CollectionIcon from "~/components/CollectionIcon"; import Fade from "~/components/Fade"; import Modal from "~/components/Modal"; import NudeButton from "~/components/NudeButton"; -import Tooltip from "~/components/Tooltip"; +import { createDocument } from "~/actions/definitions/documents"; +import useActionContext from "~/hooks/useActionContext"; import useBoolean from "~/hooks/useBoolean"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import CollectionMenu from "~/menus/CollectionMenu"; import { NavigationNode } from "~/types"; -import { newDocumentPath } from "~/utils/routeHelpers"; import DocumentLink from "./DocumentLink"; import DropCursor from "./DropCursor"; import DropToImport from "./DropToImport"; @@ -220,6 +220,10 @@ function CollectionLink({ } }, [collection.id, ui.activeCollectionId, search]); + const context = useActionContext({ + activeCollectionId: collection.id, + }); + return ( <> @@ -259,18 +263,14 @@ function CollectionLink({ !isEditing && !isDraggingAnyCollection && ( - {can.update && ( - - - - - - )} + + + ) => { - ev.preventDefault(); - ev.stopPropagation(); - - if (document.isStarred) { - document.unstar(); - } else { - document.star(); - } - }, - [document] - ); + const context = useActionContext({ + activeDocumentId: document.id, + }); if (!document) { return null; @@ -35,9 +24,9 @@ function Star({ size, document, ...rest }: Props) { return ( {document.isStarred ? ( diff --git a/app/components/Tooltip.tsx b/app/components/Tooltip.tsx index 00f4704e1f..98ae515f3e 100644 --- a/app/components/Tooltip.tsx +++ b/app/components/Tooltip.tsx @@ -3,7 +3,7 @@ import { TFunctionResult } from "i18next"; import * as React from "react"; import styled from "styled-components"; -type Props = Omit & { +export type Props = Omit & { tooltip: React.ReactChild | React.ReactChild[] | TFunctionResult; shortcut?: React.ReactNode; }; diff --git a/app/hooks/useActionContext.ts b/app/hooks/useActionContext.ts index c5be365482..e006bf1715 100644 --- a/app/hooks/useActionContext.ts +++ b/app/hooks/useActionContext.ts @@ -20,6 +20,7 @@ export default function useActionContext( return { isContextMenu: false, isCommandBar: false, + isButton: false, activeCollectionId: stores.ui.activeCollectionId, activeDocumentId: stores.ui.activeDocumentId, currentUserId: stores.auth.user?.id, diff --git a/app/types.ts b/app/types.ts index 6563d7edc4..2a7dccc2a7 100644 --- a/app/types.ts +++ b/app/types.ts @@ -69,6 +69,7 @@ export type MenuItem = export type ActionContext = { isContextMenu: boolean; isCommandBar: boolean; + isButton: boolean; activeCollectionId: string | undefined; activeDocumentId: string | undefined; currentUserId: string | undefined;