DSLib
This is an internal package with common functionality used in the Atlassian Design System Team.
This package comes with no support and semver guarantees,
your app will break if you use this directly!
Installation
yarn add @atlaskit/ds-lib
Utilities
noop()
import noop from '@atlaskit/ds-lib/noop';
noop();
warnOnce()
import warnOnce from '@atlaskit/ds-lib/warn-once';
function Component() {
if (process.env.NODE_ENV !== 'production') {
warnOnce('This component has been deprecated.');
}
return <div>This component has been deprecated</div>;
}
mergeRefs()
import mergeRefs from '@atlaskit/ds-lib/merge-refs';
const Component = forwardRef((props, ref) => {
const customRef = useRef<HTMLElement | null>(null);
return (
<div ref={mergeRefs[ref, customRef]} />
);
}
React hooks
useLazyRef()
import useLazyRef from '@atlaskit/ds-lib/use-lazy-ref';
function Component({ onClick }) {
const ref = useLazyRef(() => {
});
return <button onClick={() => onClick(ref.current)}>Click me!</button>;
}
useControlled()
import useControlled from '@atlaskit/ds-lib/use-controlled';
function ControlledComponent({ value, defaultValue = 0 }) {
const [uncontrolledState, setUncontrolledState] = useControlled(
value,
() => defaultValue,
);
return (
<button onClick={() => setUncontrolledState(uncontrolledState + 1)}>
Update state
</button>
);
}
usePreviousValue()
function Component() {
const [currentValue] = useState(1);
const previousValue = usePreviousValue(currentValue);
previousValue;
currentValue;
return null;
}
useLazyCallback()
import useLazyCallback from '@atlaskit/ds-lib/use-lazy-callback';
function Component() {
const callback = useLazyCallback(() => {
});
return null;
}
useStateRef()
import useStateRef from '@atlaskit/ds-lib/use-state-ref';
function Component() {
const [valueRef, setState] = useStateRef(0);
console.log(valueRef.current);
return <div onClick={() => setState(prev => prev + 1)} />;
useScrollbarWidth()
import useScrollbarWidth from '@atlaskit/ds-lib/use-scrollbar-width';
function Component() {
const scrollbar = useScrollbarWidth();
return (
<div id="container" style={{ padding: scrollbar.width }}>
<div id="scrollable" ref={scrollbar.ref} />
</div>
);
}
useCloseOnEscapePress()
import useCloseOnEscapePress from '@atlaskit/ds-lib/use-close-on-escape-press';
useCloseOnEscapePress({
onClose: () => {},
isDisabled: false,
});
useAutoFocus()
import useAutoFocus from '@atlaskit/ds-lib/use-auto-focus';
const elementRef = useRef();
useAutoFocus(elementRef, true);
<div ref={elementRef} />;