/* * The MIT License (MIT) * * Copyright (c) 2015 - present Instructure, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { Component, ContextType } from 'react' import { Img } from '@instructure/ui-img/latest' import { withStyleNew } from '@instructure/emotion' import { renderIconWithProps } from '@instructure/ui-icons' import generateStyles from './styles.js' import type { TreeBrowserButtonProps } from './props' import { allowedProps } from './props.js' import TreeBrowserContext from '../TreeBrowserContext.js' // Todo: merge TreeButton and TreeNode: TreeButton should be a special type of TreeNode // Map TreeBrowser size tokens to icon size tokens const treeBrowserSizeToIconSize = { small: 'md', medium: 'lg', large: 'xl' } as const /** --- parent: TreeBrowser id: TreeBrowser.Button --- **/ @withStyleNew(generateStyles, 'TreeBrowserTreeButton') class TreeButton extends Component< TreeBrowserButtonProps, { isHovered: boolean } > { static displayName = 'TreeButton' static readonly componentId = 'TreeBrowser.Button' static readonly themeId = 'TreeBrowserTreeButton' static allowedProps = allowedProps static contextType = TreeBrowserContext declare context: ContextType static defaultProps = { type: 'treeButton', size: 'medium', variant: 'folderTree', selected: false, focused: false, expanded: false, hoverable: true } ref: Element | null = null state = { isHovered: false } componentDidMount() { this.props.makeStyles?.({ animation: this.context?.animation, hoverable: this.props.hoverable }) } componentDidUpdate() { this.props.makeStyles?.({ animation: this.context?.animation, hoverable: this.props.hoverable }) } defaultContentRenderer(props: TreeBrowserButtonProps) { const { name, descriptor, styles } = props return ( {this.renderImage()} {name} {descriptor ? ( {descriptor} ) : null} ) } getIconColor(): 'baseColor' | 'onColor' | 'inverseColor' { const { selected } = this.props const { isHovered } = this.state // Priority: selected > hover > default if (selected) return 'onColor' if (isHovered) return 'inverseColor' return 'baseColor' } renderImage() { const { type } = this.props switch (type) { case 'collection': return this.renderCollectionIcon() case 'item': return this.renderItemImage() default: break } return undefined } renderCollectionIcon() { const { expanded, collectionIcon, collectionIconExpanded, styles, size } = this.props if (collectionIcon || collectionIconExpanded) { const icon = (expanded ? collectionIconExpanded : collectionIcon) as any const iconSize = treeBrowserSizeToIconSize[size!] const iconColor = this.getIconColor() const iconElement = renderIconWithProps(icon, iconSize, iconColor) return
{iconElement}
} return undefined } renderItemImage() { const { thumbnail, itemIcon, styles, size } = this.props if (thumbnail) { return (
) } if (itemIcon) { const iconSize = treeBrowserSizeToIconSize[size!] const iconColor = this.getIconColor() const iconElement = renderIconWithProps(itemIcon, iconSize, iconColor) return
{iconElement}
} return undefined } handleRef = (el: HTMLButtonElement) => { if (el && typeof this.props.containerRef === 'function') { this.props.containerRef(el.parentElement) } this.ref = el } handleMouseEnter = () => { if (this.props.hoverable) { this.setState({ isHovered: true }) } } handleMouseLeave = () => { if (this.props.hoverable) { this.setState({ isHovered: false }) } } render() { const { styles, renderContent } = this.props const buttonContent = renderContent ? renderContent(this.props) : this.defaultContentRenderer(this.props) // VoiceOver can't navigate without the buttons, even though they don't do anything return ( ) } } export default TreeButton export { TreeButton }