-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a7522a
commit c383430
Showing
11 changed files
with
298 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,114 @@ | ||
import { makePrefixer, useForkRef, useId } from "@salt-ds/core"; | ||
import { Button, makePrefixer, useForkRef } from "@salt-ds/core"; | ||
import { useComponentCssInjection } from "@salt-ds/styles"; | ||
import { useWindow } from "@salt-ds/window"; | ||
import clsx from "clsx"; | ||
import { | ||
type ComponentPropsWithoutRef, | ||
type MouseEvent, | ||
type ReactElement, | ||
forwardRef, | ||
ReactElement, | ||
ComponentPropsWithoutRef, | ||
useEffect, | ||
useId, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import clsx from "clsx"; | ||
|
||
import { useTabs } from "./TabNextContext"; | ||
import { CloseIcon } from "@salt-ds/icons"; | ||
import tabCss from "./TabNext.css"; | ||
import { useTabs } from "./TabNextContext"; | ||
|
||
const withBaseName = makePrefixer("saltTabNext"); | ||
|
||
export interface TabNextProps extends ComponentPropsWithoutRef<"button"> { | ||
export interface TabNextProps extends ComponentPropsWithoutRef<"div"> { | ||
/* Value prop is mandatory and must be unique in order for overflow to work. */ | ||
disabled?: boolean; | ||
value: string; | ||
onClose?: () => void; | ||
} | ||
|
||
export const TabNext = forwardRef<HTMLButtonElement, TabNextProps>(function Tab( | ||
props, | ||
ref | ||
): ReactElement<TabNextProps> { | ||
const { | ||
children, | ||
className, | ||
disabled: disabledProp, | ||
onClick, | ||
onFocus, | ||
value, | ||
...rest | ||
} = props; | ||
const targetWindow = useWindow(); | ||
useComponentCssInjection({ | ||
testId: "salt-tab-next", | ||
css: tabCss, | ||
window: targetWindow, | ||
}); | ||
const { | ||
disabled: tabstripDisabled, | ||
registerItem, | ||
variant, | ||
setSelected, | ||
selected, | ||
active, | ||
} = useTabs(); | ||
const disabled = tabstripDisabled || disabledProp; | ||
export const TabNext = forwardRef<HTMLDivElement, TabNextProps>( | ||
function Tab(props, ref): ReactElement<TabNextProps> { | ||
const { | ||
children, | ||
className, | ||
disabled: disabledProp, | ||
onClick, | ||
onClose, | ||
onFocus, | ||
value, | ||
...rest | ||
} = props; | ||
const targetWindow = useWindow(); | ||
useComponentCssInjection({ | ||
testId: "salt-tab-next", | ||
css: tabCss, | ||
window: targetWindow, | ||
}); | ||
const { registerItem, variant, setSelected, selected, focusInside } = | ||
useTabs(); | ||
const disabled = disabledProp; | ||
|
||
const tabRef = useRef<HTMLDivElement>(null); | ||
const handleRef = useForkRef(ref, tabRef); | ||
|
||
const handleClick = (event: MouseEvent<HTMLDivElement>) => { | ||
onClick?.(event); | ||
setSelected(value); | ||
}; | ||
|
||
useEffect(() => { | ||
return registerItem({ id: value, element: tabRef.current }); | ||
}, [value]); | ||
|
||
const closeButtonId = useId(); | ||
const labelId = useId(); | ||
|
||
const [focused, setFocused] = useState(false); | ||
|
||
const tabRef = useRef<HTMLButtonElement>(null); | ||
const handleRef = useForkRef(ref, tabRef); | ||
const handleFocus = () => { | ||
setFocused(true); | ||
}; | ||
|
||
const handleClick = (event: MouseEvent<HTMLButtonElement>) => { | ||
onClick?.(event); | ||
setSelected(value); | ||
}; | ||
const handleBlur = () => { | ||
setFocused(false); | ||
}; | ||
|
||
useEffect(() => { | ||
return registerItem({ id: value, element: tabRef.current }); | ||
}, [value]); | ||
const handleClose = (event: MouseEvent<HTMLButtonElement>) => { | ||
onClose?.(); | ||
event.stopPropagation(); | ||
}; | ||
|
||
return ( | ||
<button | ||
className={clsx(withBaseName(), withBaseName(variant), className)} | ||
data-value={value} | ||
aria-selected={selected === value || undefined} | ||
disabled={disabled} | ||
tabIndex={active === value ? 0 : -1} | ||
value={value} | ||
ref={handleRef} | ||
role="tab" | ||
type="button" | ||
onClick={handleClick} | ||
{...rest} | ||
> | ||
{children} | ||
</button> | ||
); | ||
}); | ||
return ( | ||
<div | ||
className={clsx(withBaseName(), withBaseName(variant), className)} | ||
data-value={value} | ||
aria-selected={selected === value || undefined} | ||
aria-disabled={disabled} | ||
tabIndex={!disabled ? (selected === value ? 0 : -1) : undefined} | ||
ref={handleRef} | ||
role="tab" | ||
onClick={!disabled ? handleClick : undefined} | ||
onFocus={handleFocus} | ||
onBlur={handleBlur} | ||
{...rest} | ||
> | ||
<span className={withBaseName("label")} id={labelId}> | ||
{children} | ||
</span> | ||
{onClose ? ( | ||
<Button | ||
aria-label="Dismiss" | ||
id={closeButtonId} | ||
aria-labelledby={clsx(closeButtonId, labelId)} | ||
aria-hidden={!focused} | ||
tabIndex={focused || (!focusInside && selected === value) ? 0 : -1} | ||
variant="secondary" | ||
onClick={handleClose} | ||
> | ||
<CloseIcon aria-hidden /> | ||
</Button> | ||
) : null} | ||
</div> | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.