/* * Copyright 2024 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import classNames from "classnames"; import { createElement, forwardRef, useMemo } from "react"; import { type IconName, IconNames } from "@blueprintjs/icons"; import { Classes, DISPLAYNAME_PREFIX, type MaybeElement, type Props } from "../../common"; import { H1, H2, H3, H4, H5, H6 } from "../html/html"; import { Icon } from "../icon/icon"; import { Text, type TextProps } from "../text/text"; export interface EntityTitleProps extends Props { /** * Whether the overflowing text content should be ellipsized. * * @default false */ ellipsize?: boolean; /** Whether the component should expand to fill its container. */ fill?: boolean; /** * React component to render the main title heading. This defaults to * Blueprint's `` component, * which inherits font size from its * containing element(s). * * To render larger, more prominent titles, Use Blueprint's heading * components instead (e.g. `{ H1 } from "@blueprintjs/core"`). * * @default Text */ heading?: React.FC; /** * Name of a Blueprint UI icon (or an icon element) to render in the section's header. * Note that the header will only be rendered if `title` is provided. */ icon?: IconName | MaybeElement; /** * Whether to render as loading state. * * @default false */ loading?: boolean; /** The content to render below the title. Defaults to render muted text. */ subtitle?: React.JSX.Element | string; /** The primary title to render. */ title: React.JSX.Element | string; /** If specified, the title will be wrapped in an anchor with this URL. */ titleURL?: string; /** * components work best - if multiple, wrap in */ tags?: React.ReactNode; } /** * EntityTitle component. * * @see https://blueprintjs.com/docs/#core/components/entity-title */ export const EntityTitle: React.FC = forwardRef((props, ref) => { const { className, ellipsize = false, fill = false, heading = Text, icon, loading = false, subtitle, tags, title, titleURL, } = props; const titleElement = useMemo(() => { const maybeTitleWithURL = titleURL != null ? ( {title} ) : ( title ); return createElement( heading, { className: classNames(Classes.ENTITY_TITLE_TITLE, { [Classes.SKELETON]: loading, [Classes.TEXT_OVERFLOW_ELLIPSIS]: heading !== Text && ellipsize, }), ellipsize: heading === Text ? ellipsize : undefined, }, maybeTitleWithURL, ); }, [titleURL, title, heading, loading, ellipsize]); const maybeSubtitle = useMemo(() => { if (subtitle == null) { return null; } return ( {subtitle} ); }, [ellipsize, loading, subtitle]); return (
{icon != null && (
)}
{titleElement} {tags != null &&
{tags}
}
{maybeSubtitle}
); }); EntityTitle.displayName = `${DISPLAYNAME_PREFIX}.EntityTitle`; /** * Construct header class name from H{*}. Returns `undefined` if `heading` is not a Blueprint heading. */ function getClassNameFromHeading(heading: React.FC) { const headerIndex = [H1, H2, H3, H4, H5, H6].findIndex(header => header === heading); if (headerIndex < 0) { return undefined; } return [Classes.getClassNamespace(), "entity-title-heading", `h${headerIndex + 1}`].join("-"); }