@aparajita/tailwind-ionic

中文文档
2.3.0发布于 2024/04/24MITTypeScriptCommonJS近 30 天下载 3275

Tailwind utilities for Ionic

原始使用文档
文档目录7 节
  1. Breaking changes from v1.x
  2. Installation
  3. Usage
  4. Platform/mode variants
  5. Part variants
  6. Checked variant
  7. Theme colors

@aparajita/tailwind-ionic  npm version

This plugin for Tailwind CSS and Ionic provides several features:

  • Variants which help you to target specific platforms and modes in an Ionic application.
  • A part variant to target CSS parts in an Ionic component (or any other component that uses CSS parts).
  • Ionic CSS theme variables are converted into Tailwind colors.
  • An ion-checked variant to target the checked state of an Ionic checkbox or radio button.

Breaking changes from v1.x

  • The non-abbreviated variants (prefixed with "ion-") in v1.x have been removed.
  • The ios and md variants have been renamed to mode-ios and mode-md, and only apply to the mode attribute of the html element.

Installation

pnpm add @aparajita/tailwind-ionic

If you only want the default variants and no Ionic theme colors, add the plugin to your tailwind.config.js file:

module.exports = {
  plugins: [require('@aparajita/tailwind-ionic')]
}

If you want to configure the behavior, read on.

Usage

Platform/mode variants

The platform/mode variants in the table below are supported. Variants lower in the list are more specific and are applied after variants higher in the list. This means that a less specific variant applied to a given class will be overridden by a more specific variant applied to the same class.

Note that you cannot combine variants directly, but you can combine the effect of separate variants.

VariantTarget
plt-desktopDesktop mode
plt-mobileMobile-like device (including browser simulations)
plt-mobilewebMobile device simulation mode in a browser
plt-nativeReal device using Capacitor
plt-iosiOS device (including browser simulations)
plt-androidAndroid device (including browser simulations)
mode-iosApp is in iOS style mode
mode-mdApp is in Material Design style mode

Examples (with abbreviated variant names)

<!-- BAD. Can't combine these variants with others directly. -->
<ion-label class="plt-native:plt-ios:text-ion-color-primary" />

<!-- 
  GOOD. Separate variants combine.
  On a real iOS device, bold blue color. 
  On a real Android device, bold yellow color. 
-->
<ion-label
  class="
  plt-native:font-bold
  plt-ios:text-blue-500
  plt-android:text-yellow-500
"
/>

<!-- GOOD. More specific variant overrides. On a real iOS device, red color. -->
<ion-label class="plt-native:text-blue-500 plt-ios:text-red-500" />

Part variants

The part- variant allows you to target CSS parts in an Ionic component (or any other component that uses CSS parts). All of the currently defined component parts are provided as auto-complete suggestions in your editor. Part variants can be combined with other variants.

Examples

<!-- Make a button fully rounded -->
<ion-button class="part-native:rounded-full" />

<!-- Equivalent to: -->
<ion-button class="my-button" />

<style>
  .my-button::part(native) {
    @apply rounded-full;
  }
</style>

Checked variant

The ion-checked variant allows you to target the checked state of an ion-checkbox or ion-radio. It can be combined with other variants, in particular the part- variant, to accomplish complex styling of ion-checkbox and ion-radio components entirely with Tailwind.

Examples

Here is a radio group using images for the radio buttons. The checked state is indicated by a blue ring.

Radio group with images

Here is the markup:

<ion-radio-group
  v-model="appearance"
  class="flex w-full justify-around pt-5 pb-3"
>
  <div
    v-for="info in kAppearanceInfo"
    :key="info.label"
    class="relative flex flex-col items-center"
  >
    <ion-radio
      :value="info.value"
      class="m-0 h-[80px] w-[72px] rounded-lg part-container:border-none part-mark:hidden ion-checked:ring ion-checked:ring-blue-500"
    />
    <img
      :src="info.src"
      :alt="info.label"
      class="absolute inset-0 h-[80px] w-[72px] rounded-lg border"
      :class="info.class"
    />
    <p class="mt-2 text-sm text-ion-color-dark">
      {{ info.label }}
    </p>
  </div>
</ion-radio-group>

Or, for example, if you want a checkbox to be yellow-500 in the unchecked state and indigo-500 with an indigo-400 border in the checked state, you would do this:

<ion-checkbox
  class="
    part-container:!bg-yellow-500 
    ion-checked:part-container:!bg-indigo-500 
    ion-checked:part-container:!border-indigo-400
  "
/>

Note that part-container is used to target the checkbox markup itself and ! is necessary to override the Ionic styles.

Theme colors

If you pass the plugin one or more valid paths to a CSS file containing Ionic theme variables, they are converted into Tailwind theme colors.

/** @type {import('tailwindcss/types').Config} */
/** @type {import('@aparajita/tailwind-ionic').plugin} */

const ionic = require('@aparajita/tailwind-ionic')

module.exports = {
  plugins: [ionic('src/theme/variables.css')]
}

You may also pass the path as a .theme property of an options object, or an array of strings or objects with a .theme property. This allows you to access the Ionic theme files along with your own customizations.

/** @type {import('tailwindcss/types').Config} */
/** @type {import('@aparajita/tailwind-ionic').plugin} */

const ionic = require('@aparajita/tailwind-ionic')

module.exports = {
  plugins: [ionic({
    theme: 'src/theme/variables.css',
  })]
}
/** @type {import('tailwindcss/types').Config} */
/** @type {import('@aparajita/tailwind-ionic').plugin} */

const ionic = require('@aparajita/tailwind-ionic')

module.exports = {
  plugins: [ionic([
    'src/theme/variables.css',
    'assets/css/theme.css',
    require.resolve('@ionic/vue/css/palettes/dark.class.css'),
  )]
}

Example

If the file variables.css is this:

/** Ionic CSS Variables **/
:root {
  /** primary **/
  --ion-color-primary: #3880ff;
  --ion-color-primary-rgb: 56, 128, 255;
  --ion-color-primary-contrast: #ffffff;
  --ion-color-primary-contrast-rgb: 255, 255, 255;
  --ion-color-primary-shade: #3171e0;
  --ion-color-primary-tint: #4c8dff;

  /* ...lots more */
}

then your effective Tailwind config ends up being this:

module.exports = {
  theme: {
    extend: {
      colors: {
        // ...whatever colors you have in your tailwind.config.js
        
        'ion-color-primary': 'var(--ion-color-primary)',
        'ion-color-primary-rgb': 'var(--ion-color-primary-rgb)',
        'ion-color-primary-contrast': 'var(--ion-color-primary-contrast)',
        'ion-color-primary-contrast-rgb': 'var(--ion-color-primary-contrast-rgb)',
        'ion-color-primary-shade': 'var(--ion-color-primary-shade)',
        'ion-color-primary-tint': 'var(--ion-color-primary-tint)',
        // ...and so on
      }
    }
  }
}

Because the variables are part of the color palette, they are added into all of the Tailwind color utilities: text, bg, border, etc.

<ion-label class="text-ion-color-primary">My label</ion-label>
<span class="text-ion-color-success">Success!</span>
<div class="bg-ion-color-background">
  <!-- content -->
</div>
<div class="border-ion-color-tertiary-tint">
  <!-- content -->
</div>

版本与发布记录

@aparajita/tailwind-ionic 共发布 8 个版本,首次发布于 2022/07/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.0latest2024/04/242 年前
2.2.02023/05/033 年前
2.1.02023/03/313 年前
2.0.12023/03/043 年前
2.0.02023/03/043 年前
1.1.12022/08/304 年前

关于 @aparajita/tailwind-ionic 的常见问题

@aparajita/tailwind-ionic 是什么?

Tailwind utilities for Ionic

如何安装 @aparajita/tailwind-ionic?

在项目目录运行 npm install @aparajita/tailwind-ionic,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:postcss(^8.4.21)、tailwindcss(^3.2.7)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aparajita/tailwind-ionic 支持 TypeScript 吗?

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

@aparajita/tailwind-ionic 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aparajita/tailwind-ionic 需要什么版本的 Node.js?

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

@aparajita/tailwind-ionic 可以商用吗?

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

@aparajita/tailwind-ionic 的下载量有多少?

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

@aparajita/tailwind-ionic 还在维护吗?

最新版本 2.3.0 发布于 2024/04/24(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/07/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。