Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
R
react-pull-to-refresh
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jason.yap
react-pull-to-refresh
Commits
58c27e3d
Commit
58c27e3d
authored
Apr 09, 2019
by
Guylian Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add "auto" mode for triggerHeight which triggers if the gesture is an overscroll
parent
c6f82816
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
161 additions
and
4 deletions
+161
-4
examples/Auto.tsx
examples/Auto.tsx
+66
-0
src/components/PullToRefresh.tsx
src/components/PullToRefresh.tsx
+25
-4
src/isScrollable.ts
src/isScrollable.ts
+70
-0
No files found.
examples/Auto.tsx
0 → 100644
View file @
58c27e3d
import
*
as
React
from
"
react
"
;
import
{
PullDownContent
,
PullToRefresh
,
RefreshContent
,
ReleaseContent
}
from
"
../src
"
;
export
interface
AutoProps
{
}
export
interface
AutoState
{
}
export
class
Auto
extends
React
.
Component
<
AutoProps
,
AutoState
>
{
private
onRefresh
()
{
return
new
Promise
((
resolve
)
=>
{
setTimeout
(
resolve
,
2000
);
});
}
public
render
()
{
return
(
<
div
style=
{
{
overflow
:
"
scroll
"
}
}
>
<
PullToRefresh
pullDownContent=
{
<
PullDownContent
/>
}
releaseContent=
{
<
ReleaseContent
/>
}
refreshContent=
{
<
RefreshContent
/>
}
pullDownThreshold=
{
200
}
onRefresh=
{
this
.
onRefresh
}
triggerHeight=
"auto"
backgroundColor=
"white"
>
<
div
id=
"basic-container"
>
<
div
id=
"basic-label"
>
PullToRefresh
</
div
>
</
div
>
<
div
>
<
h2
>
Scrollable content
</
h2
>
<
div
id=
"scrollable-content"
/>
</
div
>
<
style
>
{
`
#basic-container {
height: 50vh;
display: flex;
flex-direction: column;
align-items: center;
background: darkslategray;
}
#basic-label {
user-select: none;
margin-top: 20px;
color: aliceblue;
border: 1px solid aliceblue;
border-radius: 6px;
padding: 5px 2px;
}
#basic-label:hover {
cursor: pointer;
}
#scrollable-content {
height: 100vh;
width: 100%;
background: darkmagenta;
}
`
}
</
style
>
</
PullToRefresh
>
</
div
>
);
}
}
src/components/PullToRefresh.tsx
View file @
58c27e3d
import
*
as
React
from
"
react
"
;
import
{
DIRECTION
,
isTreeScrollable
}
from
"
../isScrollable
"
;
export
interface
PullToRefreshProps
{
pullDownContent
:
JSX
.
Element
;
...
...
@@ -6,7 +7,7 @@ export interface PullToRefreshProps {
refreshContent
:
JSX
.
Element
;
pullDownThreshold
:
number
;
onRefresh
:
()
=>
Promise
<
any
>
;
triggerHeight
?:
number
;
triggerHeight
?:
number
|
"
auto
"
;
backgroundColor
?:
string
;
}
...
...
@@ -81,9 +82,29 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
const
{
triggerHeight
=
40
}
=
this
.
props
;
this
.
startY
=
e
[
"
pageY
"
]
||
e
.
touches
[
0
].
pageY
;
this
.
currentY
=
this
.
startY
;
const
top
=
this
.
container
.
getBoundingClientRect
().
top
||
this
.
container
.
getBoundingClientRect
().
y
||
0
;
if
(
this
.
startY
-
top
>
triggerHeight
)
{
return
;
if
(
triggerHeight
===
"
auto
"
)
{
const
target
=
e
.
target
;
const
container
=
this
.
container
;
if
(
!
container
)
{
return
;
}
// an element we're touching can be scrolled up, so gesture is going to be a scroll gesture
if
(
e
.
type
===
"
touchstart
"
&&
isTreeScrollable
(
target
,
DIRECTION
.
up
))
{
return
;
}
// even though we're not scrolling, the pull-to-refresh isn't visible to the user so cancel
if
(
container
.
getBoundingClientRect
().
top
<
0
)
{
return
;
}
}
else
{
const
top
=
this
.
container
.
getBoundingClientRect
().
top
||
this
.
container
.
getBoundingClientRect
().
y
||
0
;
if
(
this
.
startY
-
top
>
triggerHeight
)
{
return
;
}
}
this
.
dragging
=
true
;
...
...
src/isScrollable.ts
0 → 100644
View file @
58c27e3d
export
const
DIRECTION
=
Object
.
freeze
({
up
:
-
0b01
,
down
:
0b01
,
});
function
isOverflowScrollable
(
element
:
Element
)
{
const
overflowType
=
getComputedStyle
(
element
).
overflowY
;
if
(
element
===
document
.
scrollingElement
&&
overflowType
===
"
visible
"
)
{
return
true
;
}
if
(
overflowType
!==
"
scroll
"
&&
overflowType
!==
"
auto
"
)
{
return
false
;
}
return
true
;
}
/**
* Returns whether a given element is scrollable in a given direction.
* This only checks this element, not any of its ancestors.
*
* @param {!Element} element The DOM element to check
* @param {!number} direction The direction (see {@link DIRECTION})
* @returns {!boolean} Whether the element is scrollable
*/
function
isScrollable
(
element
:
Element
,
direction
:
number
)
{
if
(
!
isOverflowScrollable
(
element
))
{
return
false
;
}
if
(
direction
===
DIRECTION
.
down
)
{
const
bottomScroll
=
element
.
scrollTop
+
element
.
clientHeight
;
return
bottomScroll
<
element
.
scrollHeight
;
}
if
(
direction
===
DIRECTION
.
up
)
{
return
element
.
scrollTop
>
0
;
}
throw
new
Error
(
"
unsupported direction
"
);
}
/**
* Returns whether a given element or any of its ancestors (up to rootElement) is scrollable in a given direction.
*
* @param {!Element} element The leaf of the DOM tree to check
* @param {!number} dir The direction (see {@link DIRECTION})
* @returns {!boolean} Whether the element or one of its ancestors is scrollable.
*/
export
function
isTreeScrollable
(
element
:
Element
,
dir
:
number
):
boolean
{
console
.
log
(
isScrollable
(
element
,
dir
),
element
);
if
(
isScrollable
(
element
,
dir
))
{
return
true
;
}
// if a body is overflow: hidden, scrolling will be disabled even though scrollingElement will report that it is not.
if
(
element
===
document
.
body
&&
getComputedStyle
(
document
.
body
).
overflowY
===
"
hidden
"
)
{
return
false
;
}
if
(
element
.
parentElement
==
null
)
{
return
false
;
}
return
isTreeScrollable
(
element
.
parentElement
,
dir
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment