@atlaskit/ds-lib

中文文档
8.0.1发布于 2026/10/22Apache-2.0TypeScriptESM + CJS近 30 天下载 625.3万

Reusable utilities and hooks specific to design system.

原始使用文档
文档目录17 节
  1. Installation
  2. Utilities
  3. noop()
  4. once()
  5. warnOnce()
  6. mergeRefs()
  7. device-check
  8. React hooks
  9. useLazyRef()
  10. useControlled()
  11. usePreviousValue()
  12. useStableRef()
  13. useLazyCallback()
  14. useStateRef()
  15. useScrollbarWidth()
  16. useCloseOnEscapePress()
  17. useAutoFocus()

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} />;

版本与发布记录

@atlaskit/ds-lib 共发布 49 个版本,首次发布于 2021/04/28;过去 12 个月发布了 7 个版本。

版本发布日期距今
8.0.1latest2026/10/2216 天前
8.0.02026/06/164 个月前
7.0.02026/04/106 个月前
6.0.02026/03/167 个月前
5.4.02026/03/137 个月前
5.3.12026/03/038 个月前

其他发布通道:next → 2.2.2。

关于 @atlaskit/ds-lib 的常见问题

@atlaskit/ds-lib 是什么?

Reusable utilities and hooks specific to design system.

如何安装 @atlaskit/ds-lib?

在项目目录运行 npm install @atlaskit/ds-lib,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atlaskit/ds-lib 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@atlaskit/ds-lib 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@atlaskit/ds-lib 可以商用吗?

@atlaskit/ds-lib 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@atlaskit/ds-lib 的下载量有多少?

根据 npm 官方下载统计,@atlaskit/ds-lib 在 2026/10/07 – 2026/11/05 这 30 天内被下载 6,252,901 次,日均约 208,430 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atlaskit/ds-lib 还在维护吗?

最新版本 8.0.1 发布于 2026/10/22(16 天前);至今共发布 49 个版本,其中过去 12 个月发布了 7 个;首次发布于 2021/04/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。