Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@atlaskit/ds-lib

atlassian2.4mApache-2.05.3.0TypeScript support: included

Reusable utilities and hooks specific to design system.

readme

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();

once()

Create a new function that only allows an existing function to be called once.

import once from '@atlaskit/ds-lib/once';

function getGreeting(name: string): string {
    return `Hello ${name}`;
}
const getGreetingOnce = once(getGreeting);

getGreetingOnce('Alex');
// getGreeting called and "Hello Alex" is returned
// "Hello Alex" is put into the cache.
// returns "Hello Alex"

getGreetingOnce('Sam');
// getGreeting is not called
// "Hello Alex" is returned from the cache.

getGreetingOnce('Greg');
// getGreeting is not called
// "Hello Alex" is returned from the cache.

Notes:

  • If the onced function throws, then the return value of the function is not cached
  • Respects call site context (this) when executing the onced function

warnOnce()

import warnOnce from '@atlaskit/ds-lib/warn-once';

function Component() {
    // Print the warning messagein in the Web console only once per session.
    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 (
    // Use the utility function to merge the forwarded ref
    // with the created ref.
    <div ref={mergeRefs[ref, customRef]} />
  );
}

device-check

import { isAppleDevice, isSafari } from '@atlaskit/ds-lib/device-check';
isAppleDevice();
isSafari();

React hooks

useLazyRef()

import useLazyRef from '@atlaskit/ds-lib/use-lazy-ref';

function Component({ onClick }) {
    // Initialize the ref
    const ref = useLazyRef(() => {
        /* Return initial data */
    });

    // Access like a normal ref
    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; // undefined
    currentValue; // 1

    return null;
}

useStableRef()

function Component({ canShow }: { canShow: () => boolean }) {
    const stableRef = useStableRef({ canShow });

    useEffect(
        () => {
            stableRef.current.canShow();
        },
        // Able to use the last render value of `canShow` without needing
        // to invalidate the effect. Useful for lazy usage of props.
        [],
    );

    return null;
}

useLazyCallback()

import useLazyCallback from '@atlaskit/ds-lib/use-lazy-callback';

function Component() {
    // `callback` always has the same reference.
    const callback = useLazyCallback(() => {
        // Watch out for its stale closure however!
    });

    return null;
}

useStateRef()

import useStateRef from '@atlaskit/ds-lib/use-state-ref';

function Component() {
  const [valueRef, setState] = useStateRef(0);

  // Access the latest value, even inside stale closures.
  console.log(valueRef.current);

  // Update state as you would with use state
  return <div onClick={() => setState(prev => prev + 1)} />;

useScrollbarWidth()

import useScrollbarWidth from '@atlaskit/ds-lib/use-scrollbar-width';

function Component() {
    const scrollbar = useScrollbarWidth();

    return (
        // Use the scrollbar width in your styles/as you wish.
        // The ref should be attached to the scrollable element.
        <div id="container" style={{ padding: scrollbar.width }}>
            <div id="scrollable" ref={scrollbar.ref} />
        </div>
    );
}

useCloseOnEscapePress()

Notice: useCloseOnEscapePress() is deprecated, Please use useCloseOnEscapePress from @atlaskit/layering instead.

import useCloseOnEscapePress from '@atlaskit/ds-lib/use-close-on-escape-press';

// Will callback when escape is pressed
useCloseOnEscapePress({
    onClose: () => {},
    isDisabled: false,
});

useAutoFocus()

import useAutoFocus from '@atlaskit/ds-lib/use-auto-focus';

const elementRef = useRef();
useAutoFocus(elementRef, true);

<div ref={elementRef} />;

changelog

@atlaskit/ds-lib

5.3.0

Minor Changes

  • 8ced6a00eae26 - Improving typing and ergonomics of mergeRefs.

    • mergeRefs will now correctly return the type of the refs passed in
    • mergeRefs will now return a React.RefCallback<T> (T being the value of the refs passed in)
    • mergeRefs now accepts null, undefined and false values, making it easier to work with when you have refs that may not exist

    Examples

    Better type inference:

    const buttonRef = createRef<HTMLButtonElement | null>();
    const mergedRef = mergeRefs([buttonRef, null, undefined, false]);
    // mergedRef is now typed as RefCallback<HTMLButtonElement | null>

    Explicit generic typing:

    const callback = mergeRefs<HTMLButtonElement | null>([buttonRef, null, undefined, false]);
    // Explicitly specify the element type for better type safety

    Flexible ref handling:

    // Now supports falsy values without TypeScript errors
    const optionalRef = condition ? someRef : false;
    const mergedRef = mergeRefs([requiredRef, optionalRef]); // ✅ Works!

5.2.0

Minor Changes

  • 07ee26a0f6e1a - Cleaned platform_only_attach_escape_handler_on_view FG. After cleaning this gate, keydown and keyup event listeners wont be attached in useCloseOnEscapePress if it is disabled in all products.

5.1.1

Patch Changes

5.1.0

Minor Changes

  • 332393d8d236d - Add forwardRefWithGeneric as a util to the ds-lib package so it is available for all of the Design System.

5.0.1

Patch Changes

5.0.0

Major Changes

  • #190351 c7fc5282f52c6 - Removing .clear() from once result functions (once was only recently added). This was done to prevent accidental exposure of .clear() to consumers when it would not be safe to clear the once cache (for example, with cleanup functions where we always want to prevent double calling). If we need a once variant in the future that requires the onced function cache to be clearable, we can create a new onceWithClear utility for that.

    import once from '@atlaskit/ds-lib/once';
    
    function getGreeting(name: string): string {
        return `Hello ${name}`;
    }
    const getGreetingOnce = once(getGreeting);
    
    // ❌ Can no longer call `.clear()` on onced functions
    getGreetingOnce.clear();

4.2.0

Minor Changes

  • #189855 75f651c9b221b - Adding a once function. once creates a new function that only allows an existing function to be called once.

    import once from '@atlaskit/ds-lib/once';
    
    function getGreeting(name: string): string {
        return `Hello ${name}`;
    }
    const getGreetingOnce = once(getGreeting);
    
    getGreetingOnce('Alex');
    // getGreeting called and "Hello Alex" is returned
    // "Hello Alex" is put into the cache.
    // returns "Hello Alex"
    
    getGreetingOnce('Sam');
    // getGreeting is not called
    // "Hello Alex" is returned from the cache.
    
    getGreetingOnce('Greg');
    // getGreeting is not called
    // "Hello Alex" is returned from the cache.

4.1.0

Minor Changes

  • #186591 4568f6d3493c7 - Adding withResolvers util which has the same behaviour as Promise.withResolvers()

    import { withResolvers } from '@atlaskit/ds-lib/with-resolvers';
    
    const { promise, resolve, reject } = withResolvers();

4.0.1

Patch Changes

4.0.0

Major Changes

Patch Changes

3.5.1

Patch Changes

  • Updated dependencies

3.5.0

Minor Changes

3.4.0

Minor Changes

3.3.0

Minor Changes

  • #168743 f7e6b20c99795 - Update IdProvider to improve compatibility with React 18: wrapped the returned children in a Fragment to ensure a single root element is always returned.

3.2.2

Patch Changes

  • #165531 57f451bda8919 - Adds side-effect config to support Compiled css extraction in third-party apps

3.2.1

Patch Changes

3.2.0

Minor Changes

  • be6f923511512 - Adding new hook: useStableRef which is helpful to store the latest values of state or props for usage in effects or event listeners without needing to create new effects or event listeners functions.

    import useStableRef from '@atlaskit/ds-lib/use-stable-ref';
    
    function Component({ canShow }: { canShow: () => boolean }) {
        const stableRef = useStableRef({ canShow });
    
        useEffect(
            () => {
                stableRef.current.canShow();
            },
            // Able to use the last render value of `canShow` without needing
            // to invalidate the effect. Useful for lazy usage of props.
            [],
        );
    
        return null;
    }

3.1.0

Minor Changes

  • #150983 a06534942509c - Export a react-uid helper for class components to access 'useId' via 'UseId' wrapping component

3.0.0

Major Changes

  • #149152 92cf54d8ca959 - This major bump exists only to make the previous version's react-uid export rename easier to consume. There are no changes in this release. See v2.7.0 for previous changes.

2.7.0

Minor Changes

  • #148281 3c4de48168ffe - Rename export of react-uid to use-id to fix import errors due to naming conflict

2.6.0

Minor Changes

  • #143323 4fdf6347eb506 - Add new entrypoint called "use-layout-effect" to ds-lib and use it inside primitives pkg.

2.5.0

Minor Changes

2.4.0

Minor Changes

  • #127511 db30e29344013 - Widening range of react and react-dom peer dependencies from ^16.8.0 || ^17.0.0 || ~18.2.0 to the wider range of ^16.8.0 || ^17.0.0 || ^18.0.0` (where applicable).

    This change has been done to enable usage of `react@18.3as well as to have a consistent peer dependency range forreactandreact-domfor/platform` packages.

2.3.1

Patch Changes

2.3.0

Minor Changes

2.2.5

Patch Changes

2.2.4

Patch Changes

2.2.3

Patch Changes

  • #32935 b1bdec7cce2 - Internal change to enforce token usage for spacing properties. There is no expected visual or behaviour change.

2.2.2

Patch Changes

2.2.1

Patch Changes

2.2.0

Minor Changes

2.1.3

Patch Changes

2.1.2

Patch Changes

2.1.1

Patch Changes

2.1.0

Minor Changes

Patch Changes

  • cd1a2f64027 - Internal code change turning on new linting rules.

2.0.1

Patch Changes

  • #22614 8a5bdb3c844 - Upgrading internal dependency (bind-event-listener) for improved internal types

2.0.0

Major Changes

  • #22029 347fd703ce0 - Removing useDocumentEvent, useWindowEvent, useElementEvent and useKeydownEvent and replacing usages with bind-event-listener

1.4.2

Patch Changes

1.4.1

Patch Changes

  • #20033 b3e5a62a9e3 - Adds static techstack to package, enforcing stricter style linting. In this case the package already satisfied this requirement so there have been no changes to styles.

1.4.0

Minor Changes

  • #16752 9f8f2b902bb - Adds a new utility function for standardised deprecation warnings.

1.3.0

Minor Changes

  • #13302 5b5bffe8f58 - - Adds two new methods: useKeyDownEvent and useFocusEvent.
    • Adds keycodes as constants.

1.2.0

Minor Changes

  • #10230 742b9d82cdc - Added useControlled hook which can be used to be handle controlled & uncontrolled behaviour of a componenent.
  • e1d9004d5ee - Adds new utility to merge refs.
  • 40d5bb8a2f4 - New useAutoFocus hook to be used when wanting to focus on an element during initial mount.
  • c60505b8a38 - Adds new useCloseOnEscapePress hook, to be used exclusively for closing layered components.
  • cade298437d - Adds three new hooks (useElementEvent, useDocumentEvent, useWindowEvent) for listening to native browser events.
  • 2d996ae3869 - Adds new hook usePreviousValue.

1.1.0

Minor Changes

  • #9756 e56d6be0379 - A new utility to calculate scrollbar width is added for re-use.
  • a2924ae3e4f - Add DS lib package with reusable utils and hooks

    • We are introducing a new package which contains reusable utils and hooks specific to design system.

    • Following hooks are available:

      • useLazyRef: Which will only run passed expensive function once and save the result into the returned ref.
    • Following utils are available:

      • noop: An empty function which returns nothing.
  • 14396cae929 - Add warnOnce helper function

  • 27570643ef2 - Adds use lazy callback and use state ref hooks.