Skip to content

Commit

Permalink
feat: add Swiper
Browse files Browse the repository at this point in the history
  • Loading branch information
zxyao145 committed Jul 24, 2023
1 parent f255fdb commit c402458
Show file tree
Hide file tree
Showing 21 changed files with 1,074 additions and 82 deletions.
9 changes: 0 additions & 9 deletions docs/BlazorMix.Docs/BlazorMix.Docs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@
<DemoSourceCode Include="Demos\**\*.razor" />
</ItemGroup>

<ItemGroup>
<DemoDocs Remove="Demos\Components\FloatingPanel\Docs\index.zh-CN.md" />
</ItemGroup>

<ItemGroup>
<DemoSourceCode Remove="Demos\Components\FloatingPanel\Demos\Demo1.razor" />
<DemoSourceCode Remove="Demos\Components\FloatingPanel\Demos\Demo2.razor" />
</ItemGroup>

<PropertyGroup>
<CliProjectDir>docs/BlazorMix.Docs.Build</CliProjectDir>
<CliPath>$(CLIProjectDir)/BlazorMix.Docs.Build.csproj</CliPath>
Expand Down
108 changes: 108 additions & 0 deletions docs/BlazorMix.Docs/Demos/Components/Swiper/Demos/Demo1.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
@inject ToastService ToastService

<DemoBlock Title="基础用法">
<Swiper>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
@onclick="@(() => { _ = ToastService.Show($"你点击了卡片 {index + 1}"); })">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

<DemoBlock Title="自动播放">
<Swiper Autoplay>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
@onclick="@(() => { _ = ToastService.Show($"你点击了卡片 {index + 1}"); })">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

<DemoBlock Title="循环">
<Swiper Loop Autoplay>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
@onclick="@(() => { _ = ToastService.Show($"你点击了卡片 {index + 1}"); })">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

<DemoBlock Title="手动控制">
<Swiper @ref="@_swiper" AllowTouchMove="@false">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
@onclick="@(() => { _ = ToastService.Show($"你点击了卡片 {index + 1}"); })">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
<Space>
<SpaceItem>
<Button OnClick="@(()=> _swiper?.SwipePrev())">
上一张
</Button>
</SpaceItem>
<SpaceItem>
<Button OnClick="@(()=> _swiper?.SwipeNext())">
下一张
</Button>
</SpaceItem>
</Space>

<DemoDescription>
在禁用手势拖拽后,可以通过 Ref 进行手动翻页
</DemoDescription>
</DemoBlock>



<DemoBlock Title="自定义样式">
<Swiper Style="@("--border-radius:8px;")" DefaultIndex="3" Loop>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
@onclick="@(() => { _ = ToastService.Show($"你点击了卡片 {index + 1}"); })">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
<DemoDescription>
通过 CSS 变量可以控制整体的圆角等样式
</DemoDescription>
</DemoBlock>

@code
{
private Swiper? _swiper;
const string ContentStyle = "height:120px; color:#fff; font-size: 48px;display: flex;justify-content: center;align-items: center;user-select: none;";
string[] _colors = new[]
{
"#ace0ff", "#bcffbd", "#e4fabd", "#ffcfac"
};

}
80 changes: 80 additions & 0 deletions docs/BlazorMix.Docs/Demos/Components/Swiper/Demos/Demo2.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<DemoBlock Title="卡在边界">
<Swiper TrackOffset="10" SlideSize="80" Autoplay
Style="@("--border-radius:8px;")">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
>
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

<DemoBlock Title="允许越过边界">
<Swiper StuckAtBoundary="false"
SlideSize="80"
DefaultIndex="3">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
>
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>


<DemoBlock Title="居中展示">
<Swiper StuckAtBoundary="false"
TrackOffset="10"
SlideSize="80">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")"
>
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>


<DemoBlock Title="循环居中展示">
<Swiper StuckAtBoundary="false"
TrackOffset="15"
SlideSize="70"
Loop
Autoplay
>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

@code
{
const string ContentStyle = "height:120px; color:#fff; font-size: 48px;display: flex;justify-content: center;align-items: center;user-select: none;";
string[] _colors = new[]
{
"#ace0ff", "#bcffbd", "#e4fabd", "#ffcfac"
};

}
97 changes: 97 additions & 0 deletions docs/BlazorMix.Docs/Demos/Components/Swiper/Demos/Demo3.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

<DemoBlock Title="指示器颜色">
<Swiper Autoplay IndicatorProps="@(new PageIndicatorProps(){ Color = "white"})">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem >
<div style="@($"{ContentStyle} background: {_colors[index]};")"
>
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
<DemoDescription>
通过 indicatorProps 可以控制指示器的外观
</DemoDescription>
</DemoBlock>


<DemoBlock Title="指示器在滑块外面">
<Swiper Autoplay
Style="@("--track-padding: 0 0 16px")">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
<DemoDescription>
通过 --track-padding 可以控制滑动轨道区域的 padding,从而实现指示器和滑块"分离"的效果
</DemoDescription>
</DemoBlock>


<DemoBlock Title="自定义指示器">
<Swiper Autoplay
Indicator="@_indicator">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>


<DemoBlock Title="无指示器">
<Swiper Autoplay NoIndicator>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

<style>
.customIndicator {
position: absolute;
top: 6px;
right: 6px;
background: rgba(0,0,0,.3);
padding: 4px 8px;
color: #fff;
border-radius: 4px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>

@code
{
const string ContentStyle = "height:120px; color:#fff; font-size: 48px;display: flex;justify-content: center;align-items: center;user-select: none;";
string[] _colors = new[]
{
"#ace0ff", "#bcffbd", "#e4fabd", "#ffcfac"
};

RenderFragment<IndicatorInfo> _indicator =
indicatorInfo => @<div class="customIndicator">@($"{indicatorInfo.Current + 1} / {indicatorInfo.Total}")</div>;

}
46 changes: 46 additions & 0 deletions docs/BlazorMix.Docs/Demos/Components/Swiper/Demos/Demo4.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<DemoBlock Title="竖向">
<Swiper Autoplay
Direction="BmDirection.Vertical"
Style="@("--height: 200px")">
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem >
<div style="@($"{ContentStyle} background: {_colors[index]};")"
>
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>


<DemoBlock Title="竖向居中">
<Swiper Autoplay
Direction="BmDirection.Vertical"
TrackOffset="10"
SlideSize="80"
StuckAtBoundary="false"
Style="@("--height: 200px")"
>
@for (int i = 0; i < _colors.Length; i++)
{
var index = i;
<SwiperItem>
<div style="@($"{ContentStyle} background: {_colors[index]};")">
@(index + 1)
</div>
</SwiperItem>
}
</Swiper>
</DemoBlock>

@code
{
const string ContentStyle = "height: 100%; color:#fff; font-size: 48px;display: flex;justify-content: center;align-items: center;user-select: none;";
string[] _colors = new[]
{
"#ace0ff", "#bcffbd", "#e4fabd", "#ffcfac"
};
}
33 changes: 33 additions & 0 deletions docs/BlazorMix.Docs/Demos/Components/Swiper/Docs/index.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Swiper 走马灯

一组轮播的区域。

## 何时使用

- 当有一组平级的内容。
- 当内容空间不足时,可以用走马灯的形式进行收纳,进行轮播展现。
- 常用于一组图片或卡片轮播。

## 示例

### 基础用法

<code-demo Src="Demos/Components/Swiper/Demos/Demo1"></code-demo>

### 不是满宽的滑块
<code-demo Src="Demos/Components/Swiper/Demos/Demo2"></code-demo>

### 指示器
<code-demo Src="Demos/Components/Swiper/Demos/Demo3"></code-demo>

### 竖向
<code-demo Src="Demos/Components/Swiper/Demos/Demo4"></code-demo>

## API

### 属性

> [xmldoc]
### CSS 变量

8 changes: 1 addition & 7 deletions src/BlazorMix.Core/BmComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ protected async Task JsInvokeAsync(string identifier, params object[] args)

protected void InvokeStateHasChanged()
{
InvokeAsync(() =>
{
if (!IsDisposed)
{
StateHasChanged();
}
});
StateHasChanged();
}

protected async Task InvokeStateHasChangedAsync()
Expand Down
Loading

0 comments on commit c402458

Please sign in to comment.