@grafana/plugin-ui

中文文档
0.18.0发布于 2026/11/11Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 24.1万

React components for Grafana plugins

原始使用文档
文档目录7 节
  1. Development
  2. Setting up local version of @grafana/plugin-ui with app plu…
  3. Dependency structure
  4. Why react and react-dom must stay in peerDependencies
  5. Deploying
  6. Grafana compatibility
  7. Storybook

plugin-ui

React components for Grafana plugins

Development

Requires running a local version of Grafana from source.

Setting up local version of @grafana/plugin-ui with app plugin

  1. Run yarn install && yarn dev
  2. Run yarn link ../path/to/plugin-ui from app plugin directory. Make sure not to commit the changes to grafana/grafana that are introduced to the package.json.
  3. Start app plugin development server.

To avoid needing to manually rebuild core grafana on every change you make in this repo while developing, instead of running yarn dev, run

yarn tsc -p ./tsconfig.build.json && yarn rollup -c rollup.config.ts --configPlugin esbuild -w --watch.onEnd="cd ../grafana/public/app/plugins/datasource/loki && yarn build

Replacing the --watch.onEnd command with whatever you are targeting.

There is a better way of doing this, when you find it, please add it here so it's not so painful working with this repo.

Dependency structure

Why react and react-dom must stay in peerDependencies

react and react-dom must be declared in peerDependencies and must never appear only in devDependencies.

This package is built with Rollup using rollup-plugin-node-externals({ deps: true, peerDeps: true }). That plugin externalizes packages listed in dependencies and peerDependencies — anything else gets bundled into the dist output.

React is not imported by this package directly, but it is resolved transitively through bundled dependencies (react-use, react-calendar, react-popper-tooltip, etc.). Because Rollup resolves CJS packages via @rollup/plugin-commonjs, any transitively-resolved React that is not marked external ends up inlined as dist/esm/node_modules/react/ in the published bundle.

When a Grafana plugin consumes this package, its webpack build follows those relative-path imports (_virtual/index.js → node_modules/react/index.js) and produces two React instances in the final bundle:

  1. Grafana's host React — loaded as an AMD external by react-dom
  2. The inlined copy from this package

React's hook dispatcher is a singleton on the React instance. react-dom renders against instance 1, but hooks called from this package's components use instance 2's dispatcher, which is null at runtime. Every hook call (useState, useEffect, …) throws:

Cannot read properties of null (reading 'useState')

Keeping react and react-dom in peerDependencies is what causes rollup-plugin-node-externals to externalize them, producing a clean dist with no inlined React.

Deploying

Increment the version in the package.json and make sure the CHANGELOG.md includes a message for each merged PR. The publish-npm workflow should trigger on version changes, which will publish the release on npm.

Grafana compatibility

@grafana/[email protected] is compatible with Grafana versions 10.4.7 and higher. If the grafanaDependency field in your plugin.json file is set to an earlier version, you should amend it to >=10.4.7 When you do that, consider also incrementing your plugin's major version to make sure users on Grafana < 10.4.7 do not accidentally install an incompatible plugin version.

Storybook

Storybook is pretty broken, components link to assets that only exist within Grafana, as such most components haven't bothered to add support for storybook.

yarn storybook

版本与发布记录

@grafana/plugin-ui 共发布 82 个版本,首次发布于 2022/10/17;过去 12 个月发布了 34 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.18.0latest2026/11/115 天前
0.17.102026/11/0511 天前
0.17.92026/10/2919 天前
0.17.82026/10/141 个月前
0.17.62026/09/241 个月前
0.17.52026/09/112 个月前

其他发布通道:dev → 0.14.0-dev.1,dsconfig → 0.16.0-dsconfig.a155abe。

关于 @grafana/plugin-ui 的常见问题

@grafana/plugin-ui 是什么?

React components for Grafana plugins

如何安装 @grafana/plugin-ui?

在项目目录运行 npm install @grafana/plugin-ui,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)、@grafana/ui(^13.0.0)、@grafana/data(^13.0.0)、@grafana/runtime(^13.0.0)、@grafana/e2e-selectors(^13.0.0)、@testing-library/react(^16.3.2)、@testing-library/jest-dom(^7.0.0)、@testing-library/user-event(^14.6.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@grafana/plugin-ui 支持 TypeScript 吗?

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

@grafana/plugin-ui 支持 ESM 还是 CommonJS?

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

@grafana/plugin-ui 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@grafana/plugin-ui 可以商用吗?

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

@grafana/plugin-ui 的下载量有多少?

根据 npm 官方下载统计,@grafana/plugin-ui 在 2026/10/17 – 2026/11/15 这 30 天内被下载 240,517 次,日均约 8,017 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@grafana/plugin-ui 还在维护吗?

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