跳过内容

开始

  1. 安装包

    终端窗口
    npm install @capgo/capacitor-sheets
  2. 注册 Web 组件

    import '@capgo/capacitor-sheets';
  3. 添加安全区域的视口设置

    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
  4. 渲染一个sheet

    <cap-sheet-trigger for="booking-sheet" action="present">Open route</cap-sheet-trigger>
    <cap-sheet id="booking-sheet" detents="18em 32em" content-placement="bottom">
    <cap-sheet-portal>
    <cap-sheet-view>
    <cap-sheet-backdrop></cap-sheet-backdrop>
    <cap-sheet-content class="route-sheet">
    <cap-sheet-bleeding-background></cap-sheet-bleeding-background>
    <cap-sheet-handle></cap-sheet-handle>
    <cap-sheet-title>Evening route</cap-sheet-title>
    <cap-sheet-description>Choose a route and confirm pickup.</cap-sheet-description>
    <cap-sheet-trigger action="dismiss">Done</cap-sheet-trigger>
    </cap-sheet-content>
    </cap-sheet-view>
    </cap-sheet-portal>
    </cap-sheet>
    .route-sheet {
    width: min(100%, 34em);
    padding: 0 1.25em 1.25em;
    }

复制到剪贴板 safe-area="auto". The sheet viewport reads both browser environment values and Capacitor fallback variables:

env(safe-area-inset-top)
env(safe-area-inset-bottom)
env(safe-area-inset-left)
env(safe-area-inset-right)
var(--safe-area-inset-top)
var(--safe-area-inset-bottom)
var(--safe-area-inset-left)
var(--safe-area-inset-right)

There is no native plugin registration step. Run only as part of your normal __CAPGO_KEEP_0__ workflow when you need to sync web assets or native project changes.

<cap-sheet safe-area="auto"></cap-sheet>
<cap-sheet safe-area="bottom left right"></cap-sheet>
<cap-sheet safe-area="none"></cap-sheet>

对于具有覆盖状态栏或系统栏的应用程序,请确保原生插件负责暴露正确的内边距值:

import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
StatusBar: {
overlaysWebView: true,
},
Keyboard: {
resize: 'body',
resizeOnFullScreen: true,
},
SystemBars: {
insetsHandling: 'css',
},
},
};
export default config;

键盘处理由 native-focus-scroll-prevention, 默认值为 true。只有当您的应用程序已经拥有键盘避免时才禁用它:

<cap-sheet native-focus-scroll-prevention="false"></cap-sheet>

所有框架助手都配置相同的自定义元素。您还可以直接控制一个sheet:

const sheet = document.querySelector('cap-sheet');
await sheet?.present();
await sheet?.stepTo(2);
await sheet?.step('down');
await sheet?.dismiss();

监听控制状态、分析或协调动画的事件:

sheet?.addEventListener('cap-sheet-presented-change', (event) => {
console.log(event.detail.presented);
});
sheet?.addEventListener('cap-sheet-active-detent-change', (event) => {
console.log(event.detail.activeDetent);
});
sheet?.addEventListener('cap-sheet-travel', (event) => {
console.log(event.detail.progress);
});

React

React
import { useEffect, useRef } from 'react';
import { setupSheet } from '@capgo/capacitor-sheets/react';
import '@capgo/capacitor-sheets';
export function BookingSheet() {
const sheetRef = useRef<HTMLElement>(null);
useEffect(() => {
if (!sheetRef.current) return;
return setupSheet(sheetRef.current, {
detents: ['18em', '32em'],
contentPlacement: 'bottom',
onPresentedChange: ({ presented }) => console.log({ presented }),
});
}, []);
return (
<cap-sheet id="booking-sheet" ref={sheetRef}>
<cap-sheet-trigger action="present">Open</cap-sheet-trigger>
<cap-sheet-view>
<cap-sheet-backdrop />
<cap-sheet-content>
<cap-sheet-handle />
<cap-sheet-title>React sheet</cap-sheet-title>
</cap-sheet-content>
</cap-sheet-view>
</cap-sheet>
);
}

@capgo/capacitor-sheets/react 也为自定义元素注册 JSX 类型。 如果 TypeScript 还报告未知标签,请在源树中添加声明文件:

src/capgo-sheets.d.ts
import '@capgo/capacitor-sheets/react';

Vue

Vue
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue';
import { setupSheet } from '@capgo/capacitor-sheets/vue';
import '@capgo/capacitor-sheets';
const sheetRef = ref<HTMLElement | null>(null);
let cleanup: (() => void) | undefined;
onMounted(() => {
if (sheetRef.value) {
cleanup = setupSheet(sheetRef.value, {
detents: ['18em', '32em'],
contentPlacement: 'bottom',
});
}
});
onUnmounted(() => cleanup?.());
</script>
<template>
<cap-sheet id="booking-sheet" ref="sheetRef">
<cap-sheet-trigger action="present">Open</cap-sheet-trigger>
<cap-sheet-view>
<cap-sheet-backdrop />
<cap-sheet-content>
<cap-sheet-handle />
<cap-sheet-title>Vue sheet</cap-sheet-title>
</cap-sheet-content>
</cap-sheet-view>
</cap-sheet>
</template>

Angular

Angular
import { AfterViewInit, Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, ViewChild } from '@angular/core';
import { setupSheet } from '@capgo/capacitor-sheets/angular';
import '@capgo/capacitor-sheets';
@Component({
selector: 'app-root',
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<cap-sheet id="booking-sheet" #sheet>
<cap-sheet-trigger action="present">Open</cap-sheet-trigger>
<cap-sheet-view>
<cap-sheet-backdrop></cap-sheet-backdrop>
<cap-sheet-content>
<cap-sheet-handle></cap-sheet-handle>
<cap-sheet-title>Angular sheet</cap-sheet-title>
</cap-sheet-content>
</cap-sheet-view>
</cap-sheet>
`,
})
export class AppComponent implements AfterViewInit {
@ViewChild('sheet', { static: true }) sheet?: ElementRef<HTMLElement>;
ngAfterViewInit(): void {
if (this.sheet?.nativeElement) {
setupSheet(this.sheet.nativeElement, {
detents: ['18em', '32em'],
contentPlacement: 'bottom',
});
}
}
}
<script lang="ts">
import { sheet } from '@capgo/capacitor-sheets/svelte';
import '@capgo/capacitor-sheets';
</script>
<cap-sheet id="booking-sheet" use:sheet={{ detents: ['18em', '32em'], contentPlacement: 'bottom' }}>
<cap-sheet-trigger action="present">Open</cap-sheet-trigger>
<cap-sheet-view>
<cap-sheet-backdrop />
<cap-sheet-content>
<cap-sheet-handle />
<cap-sheet-title>Svelte sheet</cap-sheet-title>
</cap-sheet-content>
</cap-sheet-view>
</cap-sheet>
import { onCleanup, onMount } from 'solid-js';
import { setupSheet } from '@capgo/capacitor-sheets/solid';
import '@capgo/capacitor-sheets';
export function BookingSheet() {
let sheetEl!: HTMLElement;
onMount(() => {
const cleanup = setupSheet(sheetEl, {
detents: ['18em', '32em'],
contentPlacement: 'bottom',
});
onCleanup(cleanup);
});
return (
<cap-sheet id="booking-sheet" ref={sheetEl}>
<cap-sheet-trigger action="present">Open</cap-sheet-trigger>
<cap-sheet-view>
<cap-sheet-backdrop />
<cap-sheet-content>
<cap-sheet-handle />
<cap-sheet-title>Solid sheet</cap-sheet-title>
</cap-sheet-content>
</cap-sheet-view>
</cap-sheet>
);
}
元素目的
cap-sheet弹出窗口状态、凹凸格子、手势、模态行为和事件
cap-sheet-trigger声明式的,关闭,切换,和步骤动作
cap-sheet-portal可选的覆盖层门户,用于叠加层
cap-sheet-view固定视口主机,带有安全区域和键盘填充
cap-sheet-backdrop进度同步的背景
cap-sheet-content可访问的表单面板
cap-sheet-bleeding-background背景扩展,用于圆角边缘表单
cap-sheet-handle可拖动和键盘可访问的凹点控件
cap-sheet-title可访问的标题
cap-sheet-description可访问的描述
cap-sheet-special-wrapper脱离的表单,卡片和光盒的组合钩子
cap-sheet-stack堆叠的表单分组
cap-sheet-outlet进度输出,用于深度,抖动和页面效果
cap-scroll滚动进度辅助器
cap-fixed固定层辅助器
cap-island相关浮动岛内容
cap-external-overlay覆盖内容由外部sheet树管理
选项属性默认值描述
contentPlacementcontent-placementbottomtop, bottom, left, right, 或 center
detentsdetentsnoneSpace-separated CSS lengths such as 18em 32em
safeAreasafe-areaauto保护安全区域边缘
swipeswipetrue启用指针、触摸、触摸板和轮子手势
swipeDismissalswipe-dismissaltrue允许手势在到达终点时消失 0
inertOutsideinert-outsidetrue防止在模态表单后面交互
focusTrapfocus-traptrue保持键盘焦点在表单内
closeOnOutsideClickclose-on-outside-clicktrue点击背景或视图时消失
closeOnEscapeclose-on-escapetrue按下 Esc 键时消失
nativeFocusScrollPreventionnative-focus-scroll-preventiontrue保持焦点输入项在键盘上方可见
themeColorDimmingtheme-color-dimmingauto模态时降低 WebView 主题颜色

可用 Entrypoints

可用 Entrypoints
  • @capgo/capacitor-sheets
  • @capgo/capacitor-sheets/react
  • @capgo/capacitor-sheets/vue
  • @capgo/capacitor-sheets/angular
  • @capgo/capacitor-sheets/svelte
  • @capgo/capacitor-sheets/solid

如果您正在使用 Getting Started 以规划原生插件工作为目的,连接它与 Capgo 插件目录 在 Capgo 插件目录中 Capacitor 由 Capgo 提供 在 Capacitor 由 Capgo 提供中 添加或更新插件 在添加或更新插件中 Ionic Enterprise 插件替代方案 对于Ionic Enterprise插件替代品的产品工作流程 Capgo原生构建 对于Capgo原生构建的产品工作流程