Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TextInputInnerAction): adds a tooltip-direction property to the … #3011

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/wild-fishes-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
'@primer/react': minor
---
Add the `tooltip-direction` property to the `TextInput.Action` component to make the tooltip position flexible.
21 changes: 21 additions & 0 deletions docs/content/TextInput.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ render(<WithIconAndLoadingIndicator />)
</FormControl>
```

### With tooltip direction

```jsx live
<FormControl>
<FormControl.Label>Icon action</FormControl.Label>
<TextInput
trailingAction={
<TextInput.Action
onClick={() => {
alert('clear input')
}}
icon={XIcon}
aria-label="Clear input"
tooltip-direction="nw"
edersonlucas marked this conversation as resolved.
Show resolved Hide resolved
sx={{color: 'fg.subtle'}}
/>
}
/>
</FormControl>
```

### With error and warning states

```jsx live
Expand Down
8 changes: 7 additions & 1 deletion generated/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -4118,6 +4118,12 @@
"defaultValue": "",
"description": "Text that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies."
},
{
"name": "tooltip-direction",
"type": "'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'",
"defaultValue": "n",
"description": "Sets where the tooltip renders in relation to the target."
},
{
"name": "icon",
"type": "React.FunctionComponent",
Expand Down Expand Up @@ -5100,4 +5106,4 @@
"subcomponents": []
}
}
}
}
8 changes: 7 additions & 1 deletion src/TextInput/TextInput.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
"defaultValue": "",
"description": "Text that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies."
},
{
"name": "tooltip-direction",
"type": "'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'",
"defaultValue": "n",
"description": "Sets where the tooltip renders in relation to the target."
},
{
"name": "icon",
"type": "React.FunctionComponent",
Expand Down Expand Up @@ -158,4 +164,4 @@
}
}
]
}
}
30 changes: 30 additions & 0 deletions src/TextInput/TextInput.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ export const WithTrailingAction = () => {
)
}

export const WithTooltipDirection = () => {
const [value, setValue] = useState('')

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setValue(event.target.value)
}
return (
<Box as="form">
<FormControl>
<FormControl.Label>Default label</FormControl.Label>
<TextInput
trailingAction={
<TextInput.Action
onClick={() => {
setValue('')
}}
icon={XCircleFillIcon}
aria-label="Clear input"
tooltip-direction="nw"
edersonlucas marked this conversation as resolved.
Show resolved Hide resolved
sx={{color: 'fg.subtle'}}
/>
}
value={value}
onChange={handleChange}
/>
</FormControl>
</Box>
)
}

export const WithLoadingIndicator = (args: FormControlArgs<TextInputProps>) => {
return (
<Box as="form">
Expand Down
26 changes: 22 additions & 4 deletions src/_TextInputInnerAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import {Button, IconButton, ButtonProps} from './Button'
import Tooltip from './Tooltip'
import {BetterSystemStyleObject, merge, SxProp} from './sx'

type TextInputActionProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'aria-label' | 'size'> & {
type TextInputActionProps = Omit<
React.ButtonHTMLAttributes<HTMLButtonElement>,
'aria-label' | 'size' | 'tooltip-direction'
> & {
/** @deprecated Text input action buttons should only use icon buttons */
children?: React.ReactNode
/** Text that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies. */
['aria-label']?: string
/** Position of tooltip. If no position is passed or defaults to "n" */
['tooltip-direction']?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'
/** The icon to render inside the button */
icon?: React.FunctionComponent<React.PropsWithChildren<IconProps>>
/**
Expand Down Expand Up @@ -47,13 +52,15 @@ const invisibleButtonStyleOverrides = {
const ConditionalTooltip: React.FC<
React.PropsWithChildren<{
['aria-label']?: string
['tooltip-direction']?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'
children: React.ReactNode
}>
> = ({'aria-label': ariaLabel, children}) => (
> = ({'aria-label': ariaLabel, children, 'tooltip-direction': tooltipDirection}) => (
<>
{ariaLabel ? (
<Tooltip
aria-label={ariaLabel}
direction={tooltipDirection}
sx={{
/* inline-block is used to ensure the tooltip dimensions don't
collapse when being used with `grid` or `inline` children */
Expand All @@ -69,7 +76,18 @@ const ConditionalTooltip: React.FC<
)

const TextInputAction = forwardRef<HTMLButtonElement, TextInputActionProps>(
({'aria-label': ariaLabel, children, icon, sx: sxProp, variant = 'invisible', ...rest}, forwardedRef) => {
(
{
'aria-label': ariaLabel,
'tooltip-direction': tooltipDirection,
children,
icon,
sx: sxProp,
variant = 'invisible',
...rest
},
forwardedRef,
) => {
const sx =
variant === 'invisible'
? merge<BetterSystemStyleObject>(invisibleButtonStyleOverrides, sxProp || {})
Expand All @@ -83,7 +101,7 @@ const TextInputAction = forwardRef<HTMLButtonElement, TextInputActionProps>(
return (
<Box as="span" className="TextInput-action" marginLeft={1} marginRight={1} lineHeight="0">
{icon && !children ? (
<Tooltip aria-label={ariaLabel}>
<Tooltip direction={tooltipDirection} aria-label={ariaLabel}>
<IconButton
variant={variant}
type="button"
Expand Down