🎥 Make videos programmatically with React
import React, {
createContext,
useCallback,
useContext,
useMemo,
useState,
} from 'react';
import {FAIL_COLOR, LIGHT_TEXT, WHITE} from '../../../helpers/colors';
import {Flex} from '../../layout';
import {InfoBubble} from '../InfoBubble';
import {InlineRemoveButton} from '../InlineRemoveButton';
import {getSchemaLabel} from './get-schema-label';
import {DEFAULT_PROPS_PATH_CLASSNAME} from './scroll-to-default-props-path';
import type {JSONPath} from './zod-types';
export const SchemaDescriptionOverrideContext = createContext<{
readonly jsonPath: JSONPath;
readonly description: string;
} | null>(null);
const compactStyles: React.CSSProperties = {
fontSize: 12,
color: LIGHT_TEXT,
fontFamily: 'sans-serif',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
flex: 1,
};
const descriptionStyle: React.CSSProperties = {
fontSize: 12,
maxWidth: 360,
overflowWrap: 'anywhere',
padding: 10,
};
export const SchemaLabel: React.FC<{
readonly jsonPath: JSONPath;
readonly onRemove: null | (() => void);
readonly valid: boolean;
readonly suffix: string | null;
readonly handleClick: null | (() => void);
readonly description: string | null;
}> = ({jsonPath, onRemove, valid, suffix, handleClick, description}) => {
const [clickableButtonHovered, setClickableButtonHovered] = useState(false);
const descriptionOverride = useContext(SchemaDescriptionOverrideContext);
const displayedDescription =
descriptionOverride?.jsonPath.length === jsonPath.length &&
descriptionOverride.jsonPath.every(
(part, index) => part === jsonPath[index],
)
? descriptionOverride.description
: description;
const labelStyle: React.CSSProperties = useMemo(() => {
return {
fontFamily: 'monospace',
fontSize: 12,
color: valid ? (clickableButtonHovered ? WHITE : LIGHT_TEXT) : FAIL_COLOR,
lineHeight: '20px',
};
}, [clickableButtonHovered, valid]);
const onClickablePointerEnter = useCallback(() => {
setClickableButtonHovered(true);
}, []);
const onClickablePointerLeave = useCallback(() => {
setClickableButtonHovered(false);
}, []);
const labelContent = (
<span style={labelStyle}>
{getSchemaLabel(jsonPath)} {suffix ? suffix : null}
</span>
);
return (
<div
style={compactStyles}
className={DEFAULT_PROPS_PATH_CLASSNAME}
data-json-path={jsonPath.join('.')}
>
{handleClick ? (
// Minus the padding that a button has (user agent padding-line-start)
<button
onPointerEnter={onClickablePointerEnter}
onPointerLeave={onClickablePointerLeave}
type="button"
onClick={handleClick}
style={{border: 'none', padding: 0}}
>
{labelContent}
</button>
) : (
labelContent
)}
{displayedDescription ? (
<InfoBubble aria-label="Field description" horizontalAlignment="right">
<div style={descriptionStyle}>{displayedDescription}</div>
</InfoBubble>
) : null}
<Flex />
{onRemove ? <InlineRemoveButton onClick={onRemove} /> : null}
</div>
);
};