@coveo/atomic-react

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

React specific wrapper for the Atomic component library

原始使用文档
文档目录11 节
  1. Installation
  2. Alternate installation — CDN
  3. Usage
  4. Entry points
  5. Static Assets - Languages and SVGs
  6. Result templates
  7. Styling Result Template Components
  8. Option 1 -- Using Higher-Order Components (HOC)
  9. Option 2 -- Using a style tag
  10. Localization (i18n)
  11. Usage with CDN Scripts (IIFE)

npm version

Atomic React

A React component library for building modern UIs interfacing with the Coveo platform. Atomic React is a wrapper around the core Atomic web component library.

The integration of React-based projects (using JSX) with Web based components can be tricky. This project is meant to address this issue, making it possible to use Atomic web-components in a manner that feels more natural to developers familiar with React.

Installation

npm i @coveo/atomic-react

Alternate installation — CDN

The library is also available in the Coveo CDN as an IIFE (Immediately Invoked Function Expression). This is an approach that we recommend only for quick prototyping and testing, and that can be used with very minimal front-end tooling and build tools.

We recommend against using the IIFE approach in production. The browser has to download the entire library code, regardless of which components are actually used. We rather recommend using a bundler in this situation (for example Webpack).

You can read more about this approach below.

Usage

Since Atomic React is built on top of the core Atomic web-components library, the vast majority of concepts that apply to core Atomic will apply to Atomic React.

However, there are still some special considerations.

Entry points

The @coveo/atomic-react package exposes the following entry points:

  • @coveo/atomic-react: exports the components and utilities for building non-commerce search interfaces with Atomic React.
  • @coveo/atomic-react/recommendation: exports the components and utilities for building non-commerce recommendation interfaces with Atomic React.
  • @coveo/atomic-react/commerce: exports the components and utilities for building commerce applications with Atomic React.

Static Assets - Languages and SVGs

For performance reasons, the generated JavaScript bundle does not automatically include static assets that are loaded on demand. This impacts language support, as well as the use of included SVG icons.

It is mandatory that you make available external assets distributed with Atomic React by including them in the public directory of your app. Without this, for example, labels in the app will appear as temporary placeholders.

The location of the public directory depends on how you build, configure and distribute your app.

For example, for any project created with Create React App, this would mean copying language and icon assets to the ./public directory.

cp -r node_modules/@coveo/atomic-react/dist/assets public/assets
cp -r node_modules/@coveo/atomic-react/dist/lang public/lang

It is important to respect the folder hierarchy, with SVG icons under the assets subdirectory, and labels and languages under the lang subdirectory of the public folder.

Result templates

Rendering different types of result templates based on the type of content returned by the Coveo platform is very common when building a Coveo search page.

The way to create result templates for an HTML project using the core Atomic library involves defining one or multiple atomic-result-template components, configured with HTML properties, adding conditions on the attributes and metadata of each results.

Coupled with the <template> HTML tag, this works very well in a pure HTML project.

However, this can be limiting and awkward to use in a React project using JSX.

For every kind of search interface element, Atomic React exposes a wrapper: AtomicResultList, AtomicFoldedResultList, or AtomicSearchBoxInstantResults. These wrappers require a template property that can be used in a more straightforward manner when coupled with JSX.

The template property accepts a function with either a Result parameter in the case of the AtomicResultList and AtomicSearchBoxInstantResults wrappers, or a FoldedResult parameter in the case of the AtomicFoldedResultList wrapper. Use those parameters to conditionally render different templates based on properties and fields available in result items.

The template function must then simply return a valid JSX Element.

Here is an example of a fictitious search page, which defines some premade templates for YouTube videos, as well as Salesforce cases:

import {
  Result,
  AtomicResultSectionVisual,
  AtomicResultImage,
  AtomicResultSectionTitle,
  AtomicResultLink,
  AtomicResultSectionBottomMetadata,
  AtomicText,
  AtomicResultNumber,
  AtomicFormatUnit,
  AtomicResultSectionExcerpt,
  AtomicResultText,
  AtomicResultSectionEmphasized,
  AtomicSearchInterface,
  AtomicResultList,
  buildSearchEngine,
  getSampleSearchEngineConfiguration,
} from '@coveo/atomic-react';
import {useMemo} from 'react';

const MyResultTemplateForYouTubeVideos: React.FC<{result: Result}> = ({result}) => {
  return (
    <>
      <AtomicResultSectionVisual>
        <AtomicResultImage field="ytthumbnailurl" />
      </AtomicResultSectionVisual>
      <AtomicResultSectionTitle>
        <AtomicResultLink />
      </AtomicResultSectionTitle>
      {result.raw.ytvideoduration !== undefined && (
        <AtomicResultSectionBottomMetadata>
          <AtomicText value="Duration" />
          <AtomicResultNumber field="ytvideoduration">
            <AtomicFormatUnit unit="minute" />
          </AtomicResultNumber>
        </AtomicResultSectionBottomMetadata>
      )}
    </>
  );
};

const MyResultTemplateForSalesforceCases: React.FC<{result: Result}> = ({result}) => {
  return (
    <>
      <AtomicResultSectionTitle>
        <AtomicResultLink />
      </AtomicResultSectionTitle>
      <AtomicResultSectionExcerpt>
        <AtomicResultText field="excerpt" />
      </AtomicResultSectionExcerpt>
      <AtomicResultSectionEmphasized>
        {result.raw.sfpriority !== undefined && (
          <>
            <AtomicText value="Priority" />
            <AtomicResultText field="sfpriority" />
          </>
        )}
        {result.raw.sfstatus !== undefined && (
          <>
            <AtomicText value="Status" />
            <AtomicResultText field="sfstatus" />
          </>
        )}
      </AtomicResultSectionEmphasized>
    </>
  );
};

const MyDefaultTemplate: React.FC<{}> = () => {
  return (
    <>
      <AtomicResultSectionTitle>
        <AtomicResultLink />
      </AtomicResultSectionTitle>
      <AtomicResultSectionExcerpt>
        <AtomicResultText field="excerpt" />
      </AtomicResultSectionExcerpt>
    </>
  );
};

const MyResultTemplateFunction = (result: Result) => {
  if (result.raw.filetype === 'YoutubeVideo') {
    return <MyResultTemplateForYouTubeVideos result={result} />;
  }

  if (result.raw.objecttype === 'Case') {
    return <MyResultTemplateForSalesforceCases result={result} />;
  }

  return <MyDefaultTemplate />;
};

const MyPage = () => {
  const engine = useMemo(
    () =>
      buildSearchEngine({
        configuration: getSampleSearchEngineConfiguration(),
      }),
    []
  );

  return (
    <AtomicSearchInterface engine={engine}>
      <AtomicResultList template={MyResultTemplateFunction} />
    </AtomicSearchInterface>
  );
};

Styling Result Template Components

Due to the way Atomic Web components use Shadow Dom and CSS parts to provide encapsulation, it is necessary to follow these guidelines when you wish to style elements inside any result template.

Option 1 -- Using Higher-Order Components (HOC)

This option works well if you do not need to create any CSS rule that would need to target the Shadow parts of an Atomic result component.

For example, if you want to modify the color of all result links in a template to pink, you could do so like this:

import {AtomicResultLink, AtomicSearchInterface, AtomicResultList} from '@coveo/atomic-react';
import {buildSearchEngine, getSampleSearchEngineConfiguration} from '@coveo/headless';
import {useMemo} from 'react';

const MyStyledResultLink: React.FC<React.ComponentProps<typeof AtomicResultLink>> = (props) => {
  return (
    <AtomicResultLink {...props} style={{color: 'pink'}}>
      {props.children}
    </AtomicResultLink>
  );
};

const MyPage = () => {
  const engine = useMemo(
    () =>
      buildSearchEngine({
        configuration: getSampleSearchEngineConfiguration(),
      }),
    []
  );

  return (
    <AtomicSearchInterface engine={engine}>
      <AtomicResultList
        template={(result) => {
          return <MyStyledResultLink />;
        }}
      />
    </AtomicSearchInterface>
  );
};

This approach lets you wrap any core Atomic component inside a styled one, which you can then re-use in one or more templates. This could be done with inline styling as shown here, or with more advanced techniques such as using CSS modules.

Using React.ComponentProps<typeof AnyAtomicComponent> allows you to extract any props that the core component exposes, and augment them if need be.

Option 2 -- Using a style tag

This option works in all scenarios, and allows you to target any Shadow parts that an Atomic result component exposes, in a similar manner to using plain HTML.

The following is an example that makes the text of an AtomicResultBadge pink:

import {AtomicSearchInterface, AtomicResultList, AtomicResultBadge} from '@coveo/atomic-react';
import {buildSearchEngine, getSampleSearchEngineConfiguration} from '@coveo/headless';
import {useMemo} from 'react';

const myStyles = `
atomic-result-badge::part(result-badge-element) {
    color: pink;
}
`;

const MyPage = () => {
  const engine = useMemo(
    () =>
      buildSearchEngine({
        configuration: getSampleSearchEngineConfiguration(),
      }),
    []
  );

  return (
    <AtomicSearchInterface engine={engine}>
      <AtomicResultList
        template={(result) => (
          <>
            <style>{myStyles}</style>
            <AtomicResultBadge />
          </>
        )}
      />
    </AtomicSearchInterface>
  );
};

Localization (i18n)

The Atomic React search interface component exposes an optional localization option, which takes a callback function that lets you handle localization.

import {AtomicSearchInterface} from '@coveo/atomic-react';

<AtomicSearchInterface
  localization={(i18n) => {
    i18n.addResourceBundle('en', 'translation', {
      search: "I'm feeling lucky!",
    });
  }}
></AtomicSearchInterface>;

Usage with CDN Scripts (IIFE)

For this approach to work, you need to pull different scripts in the page in the correct order, using proper external dependencies with matching versions.

First, identify the required versions of @coveo/atomic, @coveo/headless, react and react-dom that are used by @coveo-atomic-react.

You can do that by running npm view @coveo/atomic-react@latest version dependencies devDependencies peerDependencies, like so:

$ npm view @coveo/atomic-react@latest version dependencies devDependencies peerDependencies

version = '2.1.35'
dependencies = { '@coveo/atomic': '2.19.16' }
devDependencies = {
  '@coveo/headless': '2.8.10',
  '@rollup/plugin-commonjs': '^22.0.2',
  '@rollup/plugin-node-resolve': '^14.1.0',
  '@rollup/plugin-replace': '^4.0.0',
  '@rollup/plugin-typescript': '^8.5.0',
  '@types/node': '15.14.9',
  '@types/react': '18.0.17',
  '@types/react-dom': '18.0.6',
  ncp: '2.0.0',
  react: '18.2.0',
  'react-dom': '18.2.0',
  rollup: '^2.79.0',
  'rollup-plugin-polyfill-node': '^0.10.2',
  'rollup-plugin-terser': '^7.0.2'
}
peerDependencies = {
  '@coveo/headless': '^2.0.0',
  react: '>=18.0.0',
  'react-dom': '>=18.0.0'
}

In the above example, you can see that, as of the time of this writing:

  • The current latest version of @coveo/atomic-react is 2.1.35.
  • The current latest version is using @coveo/atomic at version 2.19.16.
  • The current latest version is using @coveo/headless at version 2.8.10.
  • The current latest version is using react/react-dom at version 18 and above.

Then, we need to add the required dependencies in our page. You can do that like so:

<head>
  <!-- React, ReactDOM and ReactDOMServer need to be included  -->
  <script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
  <script
    crossorigin
    src="https://unpkg.com/react-dom@18/umd/react-dom-server-legacy.browser.production.min.js"
  ></script>

  <!-- Optional script, which allows to use JSX directly in an inline script in the page -->
  <script crossorigin src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script>

  <!-- @coveo/headless need to be included as a dependency -->
  <!-- Note the matching major and minor version as explained above -->
  <script crossorigin src="https://static.cloud.coveo.com/headless/v2.8/headless.js"></script>

  <!-- @coveo/atomic need to be included as a dependency -->
  <!-- Note the matching major and minor version as explained above -->
  <script
    crossorigin
    type="module"
    src="https://static.cloud.coveo.com/atomic/v2.19/atomic.esm.js"
  ></script>

  <!--And then finally @coveo/atomic-react is included -->
  <!-- Note the matching major and minor version as explained above -->
  <script
    crossorigin
    src="https://static.cloud.coveo.com/atomic-react/v2.1/iife/atomic-react.min.js"
  ></script>
</head>

Once this is done, you can start using CoveoAtomicReact directly with an inline script tag:

<!doctype html>
<html>
  <head>
    [...]
  </head>
  <body>
    <div id="container"></div>
  </body>
  <script type="text/babel">
    'use strict';

    const {buildSearchEngine} = CoveoHeadless;

    const {
      buildSearchEngine,
      AtomicSearchInterface,
      AtomicSearchBox,
      AtomicResultList
    } = CoveoAtomicReact;

    class SearchPage extends React.Component {
      constructor(props) {
        super(props);
        // Configure engine
        this.engine = buildSearchEngine({...});
      }

      render() {
        return (
          <AtomicSearchInterface engine={this.engine}>
            <AtomicSearchBox />
            <AtomicResultList
              template={MyTemplateFunction}
            />
            [... etc ...]
          </AtomicSearchInterface>
        );
      }
    }

    const domContainer = document.querySelector('#container');
    ReactDOM.createRoot(domContainer).render(<SearchPage />);
  </script>
</html>

版本与发布记录

@coveo/atomic-react 共发布 4,434 个版本,首次发布于 2022/01/10;过去 12 个月发布了 64 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.11.51latest2026/11/091 天前
3.11.502026/11/038 天前
3.11.492026/10/2714 天前
3.11.482026/10/2022 天前
3.11.472026/10/1428 天前
3.11.462026/10/061 个月前

其他发布通道:v2-latest → 2.14.8。

关于 @coveo/atomic-react 的常见问题

@coveo/atomic-react 是什么?

React specific wrapper for the Atomic component library

如何安装 @coveo/atomic-react?

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

@coveo/atomic-react 支持 TypeScript 吗?

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

@coveo/atomic-react 支持 ESM 还是 CommonJS?

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

@coveo/atomic-react 需要什么版本的 Node.js?

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

@coveo/atomic-react 可以商用吗?

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

@coveo/atomic-react 的下载量有多少?

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

@coveo/atomic-react 还在维护吗?

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