({
tab,
range,
anchorRect,
containerRect,
viewRect,
forward,
text,
hide,
})
| 115 | hide: () => void |
| 116 | } |
| 117 | const TextSelectionMenuRenderer: React.FC<TextSelectionMenuRendererProps> = ({ |
| 118 | tab, |
| 119 | range, |
| 120 | anchorRect, |
| 121 | containerRect, |
| 122 | viewRect, |
| 123 | forward, |
| 124 | text, |
| 125 | hide, |
| 126 | }) => { |
| 127 | const setAction = useSetAction() |
| 128 | const ref = useRef<HTMLInputElement>(null) |
| 129 | const [width, setWidth] = useState(0) |
| 130 | const [height, setHeight] = useState(0) |
| 131 | const mobile = useMobile() |
| 132 | const t = useTranslation('menu') |
| 133 | |
| 134 | const cfi = tab.rangeToCfi(range) |
| 135 | const annotation = tab.book.annotations.find((a) => a.cfi === cfi) |
| 136 | const [annotate, setAnnotate] = useState(!!annotation) |
| 137 | |
| 138 | const position = forward |
| 139 | ? LayoutAnchorPosition.Before |
| 140 | : LayoutAnchorPosition.After |
| 141 | |
| 142 | const { zoom } = useTypography(tab) |
| 143 | const endContainer = forward ? range.endContainer : range.startContainer |
| 144 | const _lineHeight = parseFloat( |
| 145 | getComputedStyle(endContainer.parentElement!).lineHeight, |
| 146 | ) |
| 147 | // no custom line height and the origin is keyword, e.g. 'normal'. |
| 148 | const lineHeight = isNaN(_lineHeight) |
| 149 | ? anchorRect.height |
| 150 | : _lineHeight * (zoom ?? 1) |
| 151 | |
| 152 | return ( |
| 153 | <FocusLock disabled={mobile}> |
| 154 | <Overlay |
| 155 | // cover `sash` |
| 156 | className="!z-50 !bg-transparent" |
| 157 | onMouseDown={hide} |
| 158 | /> |
| 159 | <div |
| 160 | ref={(el) => { |
| 161 | if (!el) return |
| 162 | setWidth(el.clientWidth) |
| 163 | setHeight(el.clientHeight) |
| 164 | if (!mobile) { |
| 165 | el.focus() |
| 166 | } |
| 167 | }} |
| 168 | className={clsx( |
| 169 | 'bg-surface text-on-surface-variant shadow-1 absolute z-50 p-2 focus:outline-none', |
| 170 | )} |
| 171 | style={{ |
| 172 | left: layout(containerRect.width, width, { |
| 173 | offset: anchorRect.left + viewRect.left - containerRect.left, |
| 174 | size: anchorRect.width, |
nothing calls this directly
no test coverage detected