Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Initial amp-timeago Bento component #26507

Merged
merged 41 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f2c903d
amp-timeago v2
caroqliu Jan 27, 2020
4271a9c
Remove parseInt
caroqliu Jan 27, 2020
7c52654
Refactor logic into getFuzzyTimestampValue
caroqliu Jan 28, 2020
f979136
Lint
caroqliu Jan 28, 2020
0ec8325
Add IntersectionObserver to rerender
caroqliu Jan 28, 2020
f357dd4
Modify useIntersect
caroqliu Jan 28, 2020
1d4ed68
Refresh timestamp when entering view
caroqliu Jan 29, 2020
ec88e89
Move useIntersect
caroqliu Jan 29, 2020
170c55c
Pass props to getFuzzyTimestampValue
caroqliu Jan 29, 2020
178d13d
Use useRerenderer
caroqliu Jan 29, 2020
7546ac2
Add callback comments
caroqliu Jan 30, 2020
ef07436
Pass empty array to `useLayoutEffect`
caroqliu Jan 30, 2020
3ef8b19
Add timeago.js to build check allowlist
caroqliu Jan 30, 2020
08d869d
Remove useRerenderer and implement useInView
caroqliu Jan 30, 2020
b02dd4b
Update bind example on test page
caroqliu Jan 30, 2020
7d9c279
Correct amp-bind example text
caroqliu Jan 31, 2020
c4890a1
Refactor useIntersect as useInViewEffect
caroqliu Feb 11, 2020
45d6581
Rename use-intersect.js
caroqliu Feb 11, 2020
b8c30c2
Allowlist 3p timeago dependency
caroqliu Feb 11, 2020
b7b61f6
Restructure shape of useInViewEffect
caroqliu Feb 11, 2020
a6beb2d
Pass ref.current to useInViewEffect
caroqliu Feb 11, 2020
5755f4e
Clean up typing
caroqliu Feb 13, 2020
748f2f9
useInView -> useIntersect, useOnEnterViewport
caroqliu Feb 13, 2020
630ceb1
Reset isIntersecting in cleanup function
caroqliu Feb 14, 2020
78df30a
Restore useInView implementation
caroqliu Feb 20, 2020
4c4464a
Refactor new logic
caroqliu Feb 20, 2020
7eb6bfd
Share IntersectionOb across timeago instances
caroqliu Feb 21, 2020
9f14bfc
Replace useFnInView with useIsIntersecting
caroqliu Feb 21, 2020
225d596
Update DOM on enter viewport only
caroqliu Feb 21, 2020
ef30173
Move file + lint
caroqliu Feb 21, 2020
ea197ca
Delete file
caroqliu Feb 21, 2020
e018573
Rename timestamp to timestampRef
caroqliu Feb 21, 2020
1453b39
Initialize timestamp with null
caroqliu Feb 25, 2020
69242f2
Add type annotation to timestampRef
caroqliu Feb 26, 2020
96fba1f
Map elements to setters
caroqliu Feb 26, 2020
b112d05
Place IntersectionObserver in component
caroqliu Feb 28, 2020
395e1ae
Delete useIsIntersecting
caroqliu Feb 28, 2020
6b78d30
unobserve -> disconnect
caroqliu Feb 29, 2020
d7cd8bf
Array destructure
caroqliu Feb 29, 2020
fb36fd5
Specify props more granularly
caroqliu Mar 4, 2020
f790b35
Types
caroqliu Mar 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pass ref.current to useInViewEffect
  • Loading branch information
caroqliu committed Feb 29, 2020
commit a6beb2d0085b7a68e506a4839a8089b2cb27ebc4
14 changes: 10 additions & 4 deletions extensions/amp-timeago/0.2/timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {createElement, useState} from '../../../src/preact';
import {createElement, useRef, useState} from '../../../src/preact';
import {timeago} from '../../../third_party/timeagojs/timeago';
import {useInViewEffect} from '../../../src/preact/use-in-view';
import {useResourcesNotify} from '../../../src/preact/utils';
Expand All @@ -27,9 +27,15 @@ export function Timeago(props) {
const {0: timestamp, 1: setTimestamp} = useState(
getFuzzyTimestampValue(props)
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
);
const ref = useInViewEffect(() => {
setTimestamp(getFuzzyTimestampValue(props));
});

const ref = useRef(null);
useInViewEffect(
ref.current,
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
() => {
setTimestamp(getFuzzyTimestampValue(props));
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
},
[props]
);

useResourcesNotify();
return createElement('time', {datetime: props['datetime'], ref}, timestamp);
Expand Down
25 changes: 10 additions & 15 deletions src/preact/use-in-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,32 @@
import {useEffect, useLayoutEffect, useRef, useState} from './index';

/**
* @param {HTMLElement} node
* @param {function():(function():undefined|undefined)} effect
* @param {!Array<*>=} opt_deps
* @return {object} ref
*/
export function useInViewEffect(effect, opt_deps) {
const ref = useRef(null);
const {0: entries, 1: set} = useState([]);
export function useInViewEffect(node, effect, opt_deps) {
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
const {0: isIntersecting, 1: set} = useState(true);
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
const observerRef = useRef(null);
if (observerRef.current === null) {
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
observerRef.current = new IntersectionObserver(set);
observerRef.current = new IntersectionObserver(entries => {
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
const last = entries[entries.length - 1];
set(last.isIntersecting);
});
}

useLayoutEffect(() => {
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
// This must be done in the callback for two reasons:
// (1) ref.current changes between the call to useIntersect and this call
// (2) any updates to ref should trigger the callback to be rerun
const {current: node} = ref;
const {current: observer} = observerRef;
if (node) {
observer.observe(node);
}
return () => observer.disconnect();
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
}, [ref.current, observerRef.current]);
}, [node, observerRef.current]);

const last =
entries.length > 0 ? entries[entries.length - 1] : {isIntersecting: false};
useEffect(() => {
if (last.isIntersecting) {
if (isIntersecting) {
effect();
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
}
}, [last.isIntersecting].concat(opt_deps));

return ref;
}, [node, isIntersecting].concat(opt_deps));
caroqliu marked this conversation as resolved.
Show resolved Hide resolved
}