mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Improve display of ExternalInput on Safari
This commit is contained in:
1 file changed
+28
-6
@@ -1,4 +1,4 @@
|
||||
import * as React from "react"
|
||||
import { forwardRef, useState, useEffect, useRef } from "react"
|
||||
import { Eye, EyeOff, ExternalLink } from "lucide-react"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
@@ -11,10 +11,12 @@ interface ExternalInputProps extends Omit<React.InputHTMLAttributes<HTMLInputEle
|
||||
urlTitle?: string;
|
||||
}
|
||||
|
||||
const ExternalInput = React.forwardRef<HTMLInputElement, ExternalInputProps>(
|
||||
const ExternalInput = forwardRef<HTMLInputElement, ExternalInputProps>(
|
||||
({ className, type, url, urlTitle, value, ...props }, ref) => {
|
||||
const [showPassword, setShowPassword] = React.useState(false)
|
||||
const [isFocused, setIsFocused] = React.useState(false)
|
||||
const [showPassword, setShowPassword] = useState(false)
|
||||
const [isFocused, setIsFocused] = useState(false)
|
||||
const [buttonWidth, setButtonWidth] = useState(0)
|
||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||
|
||||
// Determine if we have a value (works for both controlled and uncontrolled)
|
||||
const hasValue = Boolean(value || (props.defaultValue && !value))
|
||||
@@ -23,15 +25,34 @@ const ExternalInput = React.forwardRef<HTMLInputElement, ExternalInputProps>(
|
||||
const showEyeToggle = isPasswordType && hasValue
|
||||
const showExternalButton = url && urlTitle && !hasValue
|
||||
|
||||
// Measure the button width when it's rendered
|
||||
useEffect(() => {
|
||||
if (showExternalButton && buttonRef.current) {
|
||||
const width = buttonRef.current.offsetWidth
|
||||
setButtonWidth(width)
|
||||
} else {
|
||||
setButtonWidth(0)
|
||||
}
|
||||
}, [showExternalButton, urlTitle])
|
||||
|
||||
// Calculate padding-right based on what's showing
|
||||
const paddingRight = showEyeToggle
|
||||
? '2.5rem' // 40px for eye toggle
|
||||
: showExternalButton && buttonWidth > 0
|
||||
? `${buttonWidth + 8}px` // button width + 8px spacing
|
||||
: undefined
|
||||
|
||||
return (
|
||||
<div className={cn("relative", className)}>
|
||||
<input
|
||||
type={isPasswordType && !showPassword ? "password" : "text"}
|
||||
className={cn(
|
||||
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-base ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
||||
(showEyeToggle || showExternalButton) && "pr-10",
|
||||
isFocused && "ring-2 ring-ring ring-offset-2"
|
||||
{
|
||||
"ring-2 ring-ring ring-offset-2": isFocused,
|
||||
},
|
||||
)}
|
||||
style={{ paddingRight }}
|
||||
ref={ref}
|
||||
value={value}
|
||||
onFocus={(e) => {
|
||||
@@ -61,6 +82,7 @@ const ExternalInput = React.forwardRef<HTMLInputElement, ExternalInputProps>(
|
||||
)}
|
||||
{showExternalButton && (
|
||||
<button
|
||||
ref={buttonRef}
|
||||
type="button"
|
||||
className="absolute right-1 top-1/2 -translate-y-1/2 inline-flex items-center gap-1 px-2 py-1 text-xs font-medium text-muted-foreground hover:text-foreground transition-colors rounded-md hover:bg-muted"
|
||||
onClick={() => window.open(url, '_blank')}
|
||||
|
||||
Reference in new issue
Block a user