Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active April 3, 2019 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/028da0610e868c520dd4f1f7e9c61b32 to your computer and use it in GitHub Desktop.
Save ryanflorence/028da0610e868c520dd4f1f7e9c61b32 to your computer and use it in GitHub Desktop.
import * as React from "react";
const { useState } = React;
const Tab = ({
disabled = false,
children,
style,
isCurrent = false,
onClick
}) => {
const [highlighted, setHighlight] = useState(false);
const [selected, setSelect] = useState(isCurrent);
// if we have an `isCurrent` we are being controlled, so ignore
// our state and use the prop
const _selected = isCurrent != null ? isCurrent : selected;
const tabStyle = {
paddingBottom: 4,
display: "block",
borderBottomStyle: "solid",
borderBottomWidth: 2,
borderBottomColor: _selected && !disabled ? "black" : "transparent",
color: _selected && !disabled ? "black" : "gray"
};
const clickHandler = () => {
// if we are not being controlled, set our state
if (isCurrent == null) setSelect(!selected);
if (onClick) onClick();
};
return (
<div
disabled={disabled}
style={style}
onMouseOver={() => setHighlight(true)}
onMouseOut={() => setHighlight(false)}
onClick={() => clickHandler()}
hover={highlighted}
>
{isCurrent.toString()}
{_selected.toString()}
<div style={tabStyle}>{React.Children.only(children)}</div>
</div>
);
};
export default Tab;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment