@beyonk/gdpr-cookie-consent-banner

中文文档
16.0.3发布于 2026/10/22MITES Module构建来源已验证近 30 天下载 8479

GDPR Cookie Consent Banner for Svelte/Kit

原始使用文档
文档目录9 节
  1. GDPR Cookie Consent banner
  2. What is it?
  3. What are its features?
  4. How do I install it?
  5. Usage
  6. Methods
  7. Props
  8. Events
  9. Fingerprinting




js-standard-style actions SvelteKit Svelte v5

What is it?

Svelte implementation of a compliant GDPR banner. It allows the user granular opt-in and out of four different cookie categories.

It now works with SvelteKit without any workarounds!

What are its features?

  • Small, discrete, and non-intrusive
  • GDPR Compliant
  • Responsive
  • Offers four categories
  • Runs any function on opting-in
  • Runs opted-in functions on each visit
  • Changing the choices requires the user to opt-in again.
  • Svelte Ready
  • No dependencies
  • Choices expire yearly
  • Optional CSS (with BEM) to bootstrap your cookie message right away
  • Modifiable labels and messages

How do I install it?

Via NPM

Simply install the node module into your codebase.

npm install --save-dev @beyonk/gdpr-cookie-consent-banner

Via CDN

Tap into either unpkg or jsdelivr:

<script
  type="module"
  src="https://cdn.jsdelivr.net/npm/@beyonk/gdpr-cookie-consent-banner/dist/index.js"
></script>

Usage

Svelte

Just use it as a regular svelte component:

<GdprBanner cookieName="foo" description="bar" on:analytics={initAnalytics} />

<script>
  import '@beyonk/gdpr-cookie-consent-banner/banner.css' // optional, you can also define your own styles
  import GdprBanner from '@beyonk/gdpr-cookie-consent-banner'

  function initAnalytics () {
    // do something with segment.io or google analytics etc
  }
</script>

HTML / Web Component

Use the custom HTML element cookie-consent-banner:

<!-- optional, you can also define your own styles, or override the provided ones -->
<link
  rel="stylesheet"
  href="//cdn.jsdelivr.net/npm/@beyonk/gdpr-cookie-consent-banner/src/lib/banner.css"
 />

<script
  type="module"
  src="https://cdn.jsdelivr.net/npm/@beyonk/gdpr-cookie-consent-banner/dist/index.js"
></script>

<cookie-consent-banner cookie-name="foo" description="bar"></cookie-consent-banner>

Methods

You can re-call the cookie banner from an external link by binding the component instance and calling show() on it.

<GdprBanner bind:this={gdprBanner} cookieName="foo" description="bar" on:analytics={initAnalytics} />

<script>
  import GdprBanner from '@beyonk/gdpr-cookie-consent-banner'

  let gdprBanner

  gdprBanner.show()
</script>

Props

The defaults are shown below.

When using as a Web Component, use the lower-dash-case version of the attribute name, pass booleans as strings, and object attributes as json strings

It is not possible to opt-out of 'necessary' cookies.

const props = {
  /**
   * You must set the cookie name.
   **/
  cookieName: 'beyonk_gdpr',

  /**
   * Whether to display the banner to the user or not. This can be because you're in an iframe
   * and there is no room to show it if they haven't already consented, but, if they have already
   * consented, you want to action the "enableAnalytics" and similar handlers if they *have*.
   *
   * Default is true
   * 
   * For Web Components, pass `visible="true"`
   **/
  visible: true,

  /**
   * The cookie configuration, such as domain and path.
   * 
   * For web components, pass a JSON string: `cookie-config='{ "domain": "example.com", "path": "/" }'`
   **/
  cookieConfig: {
    domain: 'example.com',
    path: '/'
  },

  /**
   * These are the top two lines of text on the banner
   * The 'description' field can include html such as links
   **/
  heading: 'GDPR Notice',
  description: 'We use cookies to offer a better browsing experience, analyze site traffic, personalize content, and serve targeted advertisements. Please review our <a href="/privacy-policy">privacy policy page</a>. By clicking accept, you consent to our privacy policy & use of cookies.',

  /**
   * All the button labels and aria-label content.

    Note that either acceptAllLabel or acceptSelectedLabel will appear, depending on whether the user has selected all or only some cookie choices in the configuration dialogue. 
   **/
  acceptAllLabel: 'Accept all',
  acceptSelectedLabel: 'Accept selected',
  rejectLabel: 'Reject all',
  settingsLabel: 'Preferences',
  closeLabel: 'Close window',
  editLabel: 'Edit settings',

  /**
   * These are the default opt-ins and their descriptions.
   * When value is set to true, the option will automatically be checked on load.
   *
   * If you want to hide a category, simply set it to false, e.g. 'marketing: false'
   **/
  choices: {
      necessary: {
          label: "Necessary cookies",
          description: "Used for cookie control. Can't be turned off.",
          value: true
      },
      tracking: {
          label: "Tracking cookies",
          description: "Used for advertising purposes.",
          value: true
      },
      analytics: {
          label: "Analytics cookies",
          description: "Used to control Google Analytics, a 3rd party tool offered by Google to track user behavior.",
          value: true
      },
      marketing: {
          label: "Marketing cookies",
          description: "Used for marketing data.",
          value: true
      }
  },

  /**
   * Show an icon to edit cookies later, when banner is closed.
  **/
  showEditIcon: true,

  /**
   * Show a button to reject all cookies.
  **/
  canRejectCookies: false
}

Events

Events are how you react to consent. Each type of consent emits an event when set by the user. The agreement state is passed in the event’s details.

For convenience when using Web Components - and to work around race conditions whereby events are emitted by the component prior to the handler being attached, we also emit the same events, with the prefix consent, on window.

Fingerprinting

Accepting analytics or tracking cookies will create a unique UUID to allow you to differentiate events from different users when using server-side cookies in a system such as CAPI.

This "fingerprint" is exposed as the property "fp" on the cookie.

Svelte

<GdprBanner bind:this={gdprBanner} cookieName="foo" description="bar" on:analytics={handleAnalytics} />

<script>
  import GdprBanner from '@beyonk/gdpr-cookie-consent-banner'

  function handleAnalytics (event) {
    if (event.detail.agreed) {
      // some fathom analytics tracking code or similar
    } else {
      // disable any previously initialized analytics tracking code
    }
  }
</script>

HTML / Web Components

<cookie-consent-banner cookie-name="foo" description="bar"></cookie-consent-banner>

<script>
  document.getElementsByTagName('cookie-consent-banner')[0].addEventListener('analytics', () => {
    // some fathom analytics tracking code or similar
  })

  // We also emit these events on `window`
  window.addEventListener('consent:analytics', () => {
    // some fathom analytics tracking code or similar
  })
</script>

版本与发布记录

@beyonk/gdpr-cookie-consent-banner 共发布 70 个版本,首次发布于 2018/07/30;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
16.0.3latest2026/10/2217 天前
16.0.22025/07/161.3 年前
16.0.12025/05/221.5 年前
16.0.02025/05/221.5 年前
15.0.52025/04/251.5 年前
15.0.42025/04/251.5 年前

关于 @beyonk/gdpr-cookie-consent-banner 的常见问题

@beyonk/gdpr-cookie-consent-banner 是什么?

GDPR Cookie Consent Banner for Svelte/Kit

如何安装 @beyonk/gdpr-cookie-consent-banner?

在项目目录运行 npm install @beyonk/gdpr-cookie-consent-banner,或使用 pnpm add / yarn add / bun add。

@beyonk/gdpr-cookie-consent-banner 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/beyonk__gdpr-cookie-consent-banner 是否存在。

@beyonk/gdpr-cookie-consent-banner 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@beyonk/gdpr-cookie-consent-banner 需要什么版本的 Node.js?

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

@beyonk/gdpr-cookie-consent-banner 可以商用吗?

@beyonk/gdpr-cookie-consent-banner 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beyonk/gdpr-cookie-consent-banner 的下载量有多少?

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

@beyonk/gdpr-cookie-consent-banner 还在维护吗?

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