Skip to content

Commit

Permalink
Merge 6fbaf2c into aabe4bb
Browse files Browse the repository at this point in the history
  • Loading branch information
AmelloAster committed Dec 16, 2020
2 parents aabe4bb + 6fbaf2c commit 4d12904
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 5 deletions.
105 changes: 105 additions & 0 deletions packages/tiny-swiper/demo/navigation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Demo</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}

html,
body,
.swiper-wrapper {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}

.a {
background: yellowgreen;
}

.b {
background: lightblue;
}

.c {
height: 100%;
}

.a,
.b {
display: flex;
align-items: center;
justify-content: center;
color: aliceblue;
font-size: 4rem;
}

.swiper-slide {
flex-shrink: 0;
width: 100%;
height: 100%;
}

.swiper-container {
overflow: hidden;
height: 100%;
margin-bottom: 100%;
/* touch-action: none; */
}

.swiper-wrapper {
flex-direction: column
}

.btn {
position: absolute;
top: 50%;
}

.nextEl {
right: 20px;
}

.prevEl {
left: 20px;
}
</style>
</head>
<body>
<script src="/lib/index.js"></script>
<script src="/lib/modules/navigation.js"></script>

<!-- Slider main container -->
<div class="swiper-container" id="swiper1">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="a swiper-slide">1</div>
<div class="b swiper-slide">2</div>
<div class="a swiper-slide">3</div>
<div class="b swiper-slide">4</div>
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>
<button class="nextEl btn swiper-button-disabled">++</button>
<button class="prevEl btn">--</button>
</div>

<script>
Swiper.use([ SwiperPluginNavigation ])

var mySwiper = new Swiper('#swiper1', {
navigation: {
$nextEl: '.nextEl',
$prevEl: '.prevEl'
}
});
</script>
</body>

108 changes: 108 additions & 0 deletions packages/tiny-swiper/src/modules/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
attachListener,
detachListener
} from '../core/render/dom'
import { SwiperInstance } from '../core/index'
import { Options } from '../core/options'

export type SwiperPluginNavigationOptions = Options & {
navigation: {
$nextEl: HTMLElement | string
$prevEl: HTMLElement | string
hideOnClick: boolean
disabledClass: string
hiddenClass: string
lockClass: string
}
}

export type SwiperPluginNavigationInstance = {
$nextEl: HTMLElement
$prevEl: HTMLElement
}
export default function SwiperPluginNavigation(
instance: SwiperInstance & {
navigation: SwiperPluginNavigationInstance
},
options: SwiperPluginNavigationOptions
) {
const navigation = {
$nextEl: null,
$prevEl: null
} as unknown as SwiperPluginNavigationInstance

const nextClickHandler = (e: PointerEvent) => {
const el = e.target as HTMLElement;
if (checkIsDisable(el)) {
return
}
const {
index
} = instance.state
const {
$list
} = instance.env.element

if ( index < $list.length - 1 ) {
instance.slideTo(index + 1);
}
};

const prevClickHandler = (e: PointerEvent) => {
const el = e.target as HTMLElement;
if (checkIsDisable(el)) {
return
}
const {
index
} = instance.state

if ( index > 0 ) {
instance.slideTo(index - 1);
}

};

const checkIsDisable = (e: HTMLElement) => {
if (e.className.split(' ').includes(options.navigation.disabledClass)
|| e.className.split(' ').includes(options.navigation.lockClass)) {
return true
}
return false
}

instance.on('before-init', () => {
if ( options.navigation ) {
options.navigation = Object.assign({
hideOnClick: false,
disabledClass: 'swiper-button-disabled',
hiddenClass: 'swiper-button-hidden',
lockClass: 'swiper-button-lock'
}, options.navigation)
}
})

instance.on('after-init', () => {
if ( !options.navigation ) return

navigation.$nextEl = (typeof options.navigation.$nextEl === 'string')
? document.body.querySelector(options.navigation.$nextEl) as HTMLElement
: options.navigation.$nextEl
navigation.$prevEl = (typeof options.navigation.$prevEl === 'string')
? document.body.querySelector(options.navigation.$prevEl) as HTMLElement
: options.navigation.$prevEl

attachListener(navigation.$nextEl, 'click', <EventListener> nextClickHandler)
attachListener(navigation.$prevEl, 'click', <EventListener> prevClickHandler)
})

instance.on('after-destroy', () => {
if ( !options.navigation ) return

delete navigation.$nextEl;
delete navigation.$prevEl;

detachListener(navigation.$nextEl, 'click', <EventListener> nextClickHandler)
detachListener(navigation.$prevEl, 'click', <EventListener> nextClickHandler)
})
}
41 changes: 41 additions & 0 deletions packages/website/versioned_docs/version-2.0.0-alpha/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,47 @@ You need to implement CSS code yourself.
</div>
</Demo>

### Navigation

You need to implement CSS code yourself.

<Demo
option={{
navigation: {
$nextEl: '.swiper-plugin-navigation-nextEl',
$prevEl: '.swiper-plugin-navigation-prevEl',
hideOnClick: false,
disabledClass: 'swiper-button-disabled',
hiddenClass: 'swiper-button-hidden',
lockClass: 'swiper-button-lock'
},
plugins: [
SwiperPluginNavigation
]
}}>
<div className="swiper-container">
<div className="swiper-wrapper">
<div className="swiper-slide">
<img src="https://user-images.githubusercontent.com/10026019/102231248-ba31ce00-3f28-11eb-953e-60001e810876.png"/>
</div>
<div className="swiper-slide">
<img src="https://user-images.githubusercontent.com/10026019/102232334-eef25500-3f29-11eb-857b-fcb744045fe8.png"/>
</div>
<div className="swiper-slide">
<img src="https://user-images.githubusercontent.com/10026019/102230734-32e45a80-3f28-11eb-9d78-697b8049caea.png"/>
</div>
<div className="swiper-slide">
<img src="https://user-images.githubusercontent.com/10026019/102230550-f7e22700-3f27-11eb-8d81-687c02919745.png"/>
</div>
<div className="swiper-slide">
<img src="https://user-images.githubusercontent.com/10026019/102230508-ed279200-3f27-11eb-9765-88fe733eeb8c.png"/>
</div>
</div>
<div className="swiper-plugin-navigation-nextEl"></div>
<div className="swiper-plugin-navigation-prevEl"></div>
</div>
</Demo>

### Keyboard Control

Using `up` `right` `down` `left` keys to control swiper instance.
Expand Down
5 changes: 0 additions & 5 deletions packages/website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11001,11 +11001,6 @@ tiny-invariant@^1.0.2:
resolved "https://registry.npm.taobao.org/tiny-invariant/download/tiny-invariant-1.1.0.tgz#634c5f8efdc27714b7f386c35e6760991d230875"
integrity sha1-Y0xfjv3CdxS384bDXmdgmR0jCHU=

tiny-swiper@^2.0.0-alpha.0:
version "2.0.0-alpha.0"
resolved "https://registry.yarnpkg.com/tiny-swiper/-/tiny-swiper-2.0.0-alpha.0.tgz#1083ff968d43edbcc73b9815bb3f5d05a0d30a65"
integrity sha512-bLN3KPxIga00RrTj5uj/3xP/YbdfKQsjnGnZToSo1kn6RLfSQBzuQJa8HhWKZk9uuSOD1q6B1dah0ecSOxtBOQ==

tiny-warning@^1.0.0, tiny-warning@^1.0.3:
version "1.0.3"
resolved "https://registry.npm.taobao.org/tiny-warning/download/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
Expand Down

0 comments on commit 4d12904

Please sign in to comment.