/* * Copyright (C) 2022 Kristóf Marussy * * This file is part of Sophie. * * Sophie is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * SPDX-License-Identifier: AGPL-3.0-only */ import { styled } from '@mui/material/styles'; import React, { forwardRef, ForwardedRef } from 'react'; const inputClassName = 'LocationOverlayInput-Input'; const overlayClassName = 'LocationOverlayInput-Overlay'; const clipClassName = 'LocationOverlayInput-Clip'; const LocationOverlayInputRoot = styled('div', { name: 'LocationOverlayInput', slot: 'Root', shouldForwardProp: (prop) => prop !== 'overlayVisible', })<{ overlayVisible: boolean }>(({ theme, overlayVisible }) => { const itemStyle = { padding: '6px 0 7px 0', }; return { display: 'flex', position: 'relative', flex: 1, [`.${inputClassName}`]: { ...itemStyle, color: overlayVisible ? 'transparent' : 'inherit', }, [`.${overlayClassName}`]: { ...itemStyle, display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, // Text rendering with selected transparent text works better on the bottom in light mode. zIndex: theme.palette.mode === 'dark' ? 999 : -999, pointerEvents: 'none', width: 'auto', }, [`.${clipClassName}`]: { flex: 1, overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'clip', }, }; }); export interface LocationOverlayInputProps extends React.HTMLProps { className?: string | undefined; overlayVisible?: boolean; overlay?: JSX.Element | undefined; } const LocationOverlayInput = forwardRef( ( { overlayVisible, overlay, className, ...props }: LocationOverlayInputProps, ref: ForwardedRef, ) => ( {/* eslint-disable react/jsx-props-no-spreading -- Deliberately passing props through to the actual input element. */} {/* eslint-enable react/jsx-props-no-spreading */} {overlayVisible && (
{overlay}
)}
), ); LocationOverlayInput.displayName = 'LocationOverlayInput'; LocationOverlayInput.defaultProps = { className: undefined, overlayVisible: false, overlay: undefined, }; export default LocationOverlayInput;