03基準確定 — 引いた距離の値が基準を超えると確定状態に変わるトリガー
引いた距離の値が決まった基準の値を超えた状態で指を離した状態になると確定した状態に変わって更新が始まり、基準に届かないまま指を離した状態になると距離の値は0に戻ります。
使いどころメールボックスの更新、SNSタイムラインの更新
ONOFFsetCandidate

<main class="stage">
<div class="prtc is-demo" id="prtc">
<div class="prtc__phone" id="prtcPhone">
<div class="prtc__zone">
<span class="prtc__disc" id="prtcDisc"></span>
</div>
<div class="prtc__list" id="prtcList">
<div class="prtc__row"></div>
<div class="prtc__row"></div>
<div class="prtc__row"></div>
</div>
</div>
<span class="prtc__hint" id="prtcHint">下に引っぱってください</span>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 계산 겹(히스테리시스 setCandidate())은 JS 에 있다 — 여기는 정적 배치만.
@include stage($stage-paper);
.prtc {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
gap: $sp-3;
}
.prtc__phone {
position: relative;
width: 220px;
height: 168px;
overflow: hidden;
border-radius: $r-card;
background: $subject-cream;
touch-action: none;
cursor: grab;
}
.prtc__zone {
position: relative;
height: 56px;
display: grid;
place-items: center;
}
.prtc__disc {
width: 28px;
height: 28px;
border-radius: $r-pill;
background: rgba($subject-ink, .22);
transition: background $dur-quick $ease-out;
&--ready { background: $subject-blue-deep; }
&--spin { animation: prtcSpin .6s linear infinite; }
}
@keyframes prtcSpin {
from { transform: rotate(0deg) scale(1); }
to { transform: rotate(360deg) scale(1); }
}
.prtc__list {
position: relative;
display: flex;
flex-direction: column;
gap: $sp-2;
padding: $sp-3 $sp-4;
}
.prtc__row {
height: 20px;
border-radius: $r-sm;
background: $stage-paper;
}
.prtc__hint {
color: rgba($subject-ink, .72);
font-size: 12px;
text-align: center;
}
@media (max-width: 340px) {
.prtc__phone { width: 176px; height: 132px; }
.prtc__zone { height: 44px; }
.prtc__disc { width: 22px; height: 22px; }
.prtc__list { padding: $sp-2 $sp-3; gap: $sp-1; }
.prtc__row { height: 16px; }
}
@media (prefers-reduced-motion: reduce) {
.prtc__disc, .prtc__list { transition: none !important; animation: none !important; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffffff;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.prtc {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
}
.prtc__phone {
position: relative;
width: 220px;
height: 168px;
overflow: hidden;
border-radius: 16px;
background: #fff7e6;
touch-action: none;
cursor: grab;
}
.prtc__zone {
position: relative;
height: 56px;
display: grid;
place-items: center;
}
.prtc__disc {
width: 28px;
height: 28px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.22);
transition: background 220ms ease-out;
}
.prtc__disc--ready {
background: #0a4fe8;
}
.prtc__disc--spin {
animation: prtcSpin 0.6s linear infinite;
}
@keyframes prtcSpin {
from {
transform: rotate(0deg) scale(1);
}
to {
transform: rotate(360deg) scale(1);
}
}
.prtc__list {
position: relative;
display: flex;
flex-direction: column;
gap: 8px;
padding: 12px 16px;
}
.prtc__row {
height: 20px;
border-radius: 8px;
background: #ffffff;
}
.prtc__hint {
color: rgba(23, 20, 26, 0.72);
font-size: 12px;
text-align: center;
}
@media (max-width: 340px) {
.prtc__phone {
width: 176px;
height: 132px;
}
.prtc__zone {
height: 44px;
}
.prtc__disc {
width: 22px;
height: 22px;
}
.prtc__list {
padding: 8px 12px;
gap: 4px;
}
.prtc__row {
height: 16px;
}
}
@media (prefers-reduced-motion: reduce) {
.prtc__disc, .prtc__list {
transition: none !important;
animation: none !important;
}
}(function () {
var phone = document.getElementById('prtcPhone');
var disc = document.getElementById('prtcDisc');
var list = document.getElementById('prtcList');
var hint = document.getElementById('prtcHint');
var root = document.getElementById('prtc');
// 계산 겹: 확정 여부(committed candidate)는 raw 하나의 부등호가 아니라 두 문턱의 히스테리시스로 정한다.
// ON(100px) 을 넘으면 true 로, 다시 OFF(70px) 아래로 내려가야만 false 로 — 경계 근처에서 깜빡이지 않는다.
var ON = 100, OFF = 70, MAX = 160;
var candidate = false, state = 'idle'; // idle -> dragging -> committing -> idle
function setCandidate(raw) {
if (!candidate && raw >= ON) { candidate = true; }
else if (candidate && raw < OFF) { candidate = false; }
}
function apply(raw) {
setCandidate(raw);
var pull = Math.min(raw, MAX) * 0.3;
disc.style.transform = 'translateY(' + pull.toFixed(1) + 'px) scale(' + (0.7 + 0.3 * Math.min(1, raw / ON)).toFixed(2) + ')';
disc.classList.toggle('prtc__disc--ready', candidate);
list.style.transform = 'translateY(' + pull.toFixed(1) + 'px)';
hint.textContent = candidate ? '離すと更新' : (raw > 4 ? 'もっと引っぱってください (' + Math.round(raw) + '/' + ON + 'px)' : '下に引っぱってください');
}
function commit() {
state = 'committing';
hint.textContent = '更新中';
disc.classList.add('prtc__disc--spin');
setTimeout(function () {
disc.classList.remove('prtc__disc--spin', 'prtc__disc--ready');
candidate = false; state = 'idle';
hint.textContent = '下に引っぱってください';
apply(0);
}, 620);
}
var dragging = false, startY = 0, startRaw = 0, raw = 0, target = 0, raf = null;
function settle() {
raw += (target - raw) * 0.3;
if (Math.abs(target - raw) < 0.3) { raw = target; }
apply(raw);
if (raw !== target) { raf = requestAnimationFrame(settle); } else { raf = null; }
}
function goTo(v) { target = v; if (raf === null) { raf = requestAnimationFrame(settle); } }
function onDown(y) {
if (state !== 'idle') { return; }
dragging = true; state = 'dragging'; startY = y; startRaw = raw;
if (raf) { cancelAnimationFrame(raf); raf = null; }
}
function onMove(y) {
if (!dragging) { return; }
raw = Math.max(0, Math.min(MAX, startRaw + (y - startY)));
apply(raw);
}
function onUp() {
if (!dragging) { return; }
dragging = false;
if (candidate) { commit(); } else { state = 'idle'; goTo(0); }
}
function wireReal() {
phone.addEventListener('pointerdown', function (e) { root.classList.remove('is-demo'); stopAutoplay(); onDown(e.clientY); });
window.addEventListener('pointermove', function (e) { onMove(e.clientY); });
window.addEventListener('pointerup', onUp);
}
phone.style.touchAction = 'none';
wireReal();
apply(0);
// 자동 루프: raw 를 단계적으로 MAX 까지 올려 같은 setCandidate()·commit() 경로를 탄다.
var autoRaw = 0, autoTimer = null;
function autoTick() {
if (state !== 'idle' && state !== 'dragging') { return; }
state = 'dragging';
autoRaw += 28;
if (autoRaw >= MAX) {
raw = MAX; apply(raw); autoRaw = 0;
if (candidate) { commit(); } else { state = 'idle'; goTo(0); }
return;
}
raw = autoRaw; apply(raw);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
function stopAutoplay() { if (autoTimer) { clearInterval(autoTimer); autoTimer = null; } }
if (!reduce) { autoTimer = setInterval(autoTick, 170); }
})();import { useEffect, useRef } from "react";
import styles from "./PullRefreshThresholdCommit.module.scss";
export type PullRefreshThresholdCommitProps = {
color?: string;
};
export default function PullRefreshThresholdCommit({
color = "#0a4fe8",
}: PullRefreshThresholdCommitProps) {
const rootRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const phone = root.querySelector<HTMLDivElement>(`.${styles.phone}`);
const disc = root.querySelector<HTMLSpanElement>(`.${styles.disc}`);
const list = root.querySelector<HTMLDivElement>(`.${styles.list}`);
const hint = root.querySelector<HTMLSpanElement>(`.${styles.hint}`);
if (!phone || !disc || !list || !hint) return;
// 계산 겹: 확정 여부(committed candidate)는 raw 하나의 부등호가 아니라 두 문턱의 히스테리시스로 정한다.
// ON(100px) 을 넘으면 true 로, 다시 OFF(70px) 아래로 내려가야만 false 로 — 경계 근처에서 깜빡이지 않는다.
const ON = 100, OFF = 70, MAX = 160;
let candidate = false;
let state: "idle" | "dragging" | "committing" = "idle";
const readyClass = styles["disc--ready"];
const spinClass = styles["disc--spin"];
function setCandidate(raw: number) {
if (!candidate && raw >= ON) { candidate = true; }
else if (candidate && raw < OFF) { candidate = false; }
}
function apply(raw: number) {
setCandidate(raw);
const pull = Math.min(raw, MAX) * 0.3;
disc!.style.transform = "translateY(" + pull.toFixed(1) + "px) scale(" + (0.7 + 0.3 * Math.min(1, raw / ON)).toFixed(2) + ")";
disc!.classList.toggle(readyClass, candidate);
list!.style.transform = "translateY(" + pull.toFixed(1) + "px)";
hint!.textContent = candidate ? "놓으면 새로고침" : (raw > 4 ? "더 당기세요 (" + Math.round(raw) + "/" + ON + "px)" : "아래로 당기세요");
}
let commitTimer: number | null = null;
function commit() {
state = "committing";
hint!.textContent = "새로고침 중";
disc!.classList.add(spinClass);
commitTimer = window.setTimeout(() => {
disc!.classList.remove(spinClass, readyClass);
candidate = false; state = "idle";
hint!.textContent = "아래로 당기세요";
apply(0);
}, 620);
}
let dragging = false, startY = 0, startRaw = 0, raw = 0, target = 0, raf: number | null = null;
function settle() {
raw += (target - raw) * 0.3;
if (Math.abs(target - raw) < 0.3) { raw = target; }
apply(raw);
raf = raw !== target ? requestAnimationFrame(settle) : null;
}
function goTo(v: number) { target = v; if (raf === null) { raf = requestAnimationFrame(settle); } }
function onDown(y: number) {
if (state !== "idle") return;
dragging = true; state = "dragging"; startY = y; startRaw = raw;
if (raf !== null) { cancelAnimationFrame(raf); raf = null; }
}
function onMove(y: number) {
if (!dragging) return;
raw = Math.max(0, Math.min(MAX, startRaw + (y - startY)));
apply(raw);
}
function onUp() {
if (!dragging) return;
dragging = false;
if (candidate) { commit(); } else { state = "idle"; goTo(0); }
}
function onPointerDown(e: PointerEvent) { stopAutoplay(); onDown(e.clientY); }
function onPointerMove(e: PointerEvent) { onMove(e.clientY); }
phone.addEventListener("pointerdown", onPointerDown);
window.addEventListener("pointermove", onPointerMove);
window.addEventListener("pointerup", onUp);
apply(0);
let autoRaw = 0, autoTimer: number | null = null;
function autoTick() {
if (state !== "idle" && state !== "dragging") return;
state = "dragging";
autoRaw += 28;
if (autoRaw >= MAX) {
raw = MAX; apply(raw); autoRaw = 0;
if (candidate) { commit(); } else { state = "idle"; goTo(0); }
return;
}
raw = autoRaw; apply(raw);
}
function stopAutoplay() {
if (autoTimer !== null) { window.clearInterval(autoTimer); autoTimer = null; }
if (commitTimer !== null) { window.clearTimeout(commitTimer); }
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (!reduce) { autoTimer = window.setInterval(autoTick, 170); }
return () => {
stopAutoplay();
if (raf !== null) { cancelAnimationFrame(raf); }
phone.removeEventListener("pointerdown", onPointerDown);
window.removeEventListener("pointermove", onPointerMove);
window.removeEventListener("pointerup", onUp);
};
}, []);
return (
<div ref={rootRef} className={styles.stage}>
<div className={styles.prtc}>
<div className={styles.phone}>
<div className={styles.zone}>
<span className={styles.disc} style={{ "--accent": color } as any} />
</div>
<div className={styles.list}>
<div className={styles.row} />
<div className={styles.row} />
</div>
</div>
<span className={styles.hint}>아래로 당기세요</span>
</div>
</div>
);
}03 基準確定 — 引いた距離の値が基準を超えると確定状態に変わるトリガー — 計測 累積面積 13.559% · 動いたフレーム 21/23GODRICH オリジナル · SCSS と React は zip に同梱 · 引っ張って更新 スマホ 9選 — 引いた距離で変わる更新のかたち
このデモが属する記事
引っ張って更新 スマホ 9選 — 引いた距離で変わる更新のかたち9つのうち1つは引いた距離の値が大きくなるほど実際に動く幅の値がだんだん狭くなり、1つはその距離の割合の値だけ回転の値も一緒に回り、もう1つは基準を超えた状態で指を離してはじめて確定した状態に変わります。引っ張って更新 スマホの9パターンで この記事のデモをすべて見る


