-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Every scrollable components - `ScrollView`, `FlatList`, `SectionList` and `KeyboardAwareScrollView` - now optionally report their scroll position to a shared context. This scroll context can then be used by custom elements. This PR adds to the demo app a new `scroll-opacity` element that syncs its opacity with the position of the scroll view. A practical example is added to make the title of a page fade in/out as it leaves/comes back into view. Implementation details: see commits breakdown. https://github.com/user-attachments/assets/f84ad40d-0b07-4b2a-a871-c9f4a9638eb6 --------- Co-authored-by: flochtililoch <flochtililoch@gmail.com>
- Loading branch information
1 parent
9b50ce0
commit e65f2b6
Showing
21 changed files
with
344 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
demo/backend/advanced/community/elements/scroll-opacity/_styles.xml.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<style | ||
id="text" | ||
marginHorizontal="24" | ||
marginBottom="16" | ||
/> |
80 changes: 80 additions & 0 deletions
80
demo/backend/advanced/community/elements/scroll-opacity/index.xml.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
permalink: "/backend/advanced/community/scroll-opacity.xml" | ||
tags: "Advanced/Community/Elements" | ||
hv_title: "Scroll Opacity" | ||
--- | ||
|
||
{% extends 'templates/base.xml.njk' %} | ||
|
||
{% block styles %} | ||
{% include './_styles.xml.njk' %} | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<header style="header"> | ||
<view action="back" href="#" style="header-btn"> | ||
{% include 'icons/back.svg' %} | ||
</view> | ||
<scroll-opacity:scroll-opacity | ||
xmlns:scroll-opacity="https://hyperview.org/scroll-opacity" | ||
scroll-opacity:context-key="scroll-view" | ||
scroll-opacity:distance="20" | ||
scroll-opacity:initial-opacity="0" | ||
> | ||
<text style="header-title" key="title">{{ hv_title }}</text> | ||
</scroll-opacity:scroll-opacity> | ||
</header> | ||
<view | ||
style="main" | ||
xmlns:scroll="https://hyperview.org/hyperview-scroll" | ||
scroll="true" | ||
scroll:context-key="scroll-view" | ||
scroll:event-throttle="24" | ||
> | ||
<text style="header-title text">{{ hv_title }}</text> | ||
<text style="text"> | ||
Velit voluptas et alias atque provident sapiente consequuntur deserunt. Dolorem et | ||
non error dolorem voluptate amet accusantium. Corporis rerum sed labore quae sed qui quis quasi. Illo pariatur sint qui. | ||
Quasi quaerat id molestias. Necessitatibus et ipsa quia asperiores laborum neque. Quisquam dolorem consequatur illum. Ut | ||
magni iusto explicabo blanditiis quasi laborum incidunt earum. Eius ut in rerum ipsam. Officiis dolores suscipit | ||
consequatur placeat commodi eum. Vel possimus placeat aut eos tempore saepe. Esse assumenda eum illo sed aut earum quia | ||
voluptatibus. Recusandae qui iusto corporis sed atque. Veniam et possimus praesentium. Cum molestiae non velit minus | ||
voluptatibus quos illo sed. Et omnis ut soluta qui inventore molestias. Dolores voluptatem perspiciatis exercitationem | ||
consectetur minus illum. Quos quaerat omnis aut eius eos dolores velit. Et quia vel ea unde eum repudiandae. Repellat et | ||
ab sed. | ||
</text> | ||
<text style="text"> | ||
Velit voluptas et alias atque provident sapiente consequuntur deserunt. Dolorem et | ||
non error dolorem voluptate amet accusantium. Corporis rerum sed labore quae sed qui quis quasi. Illo pariatur sint qui. | ||
Quasi quaerat id molestias. Necessitatibus et ipsa quia asperiores laborum neque. Quisquam dolorem consequatur illum. Ut | ||
magni iusto explicabo blanditiis quasi laborum incidunt earum. Eius ut in rerum ipsam. Officiis dolores suscipit | ||
consequatur placeat commodi eum. Vel possimus placeat aut eos tempore saepe. Esse assumenda eum illo sed aut earum quia | ||
voluptatibus. Recusandae qui iusto corporis sed atque. Veniam et possimus praesentium. Cum molestiae non velit minus | ||
voluptatibus quos illo sed. Et omnis ut soluta qui inventore molestias. Dolores voluptatem perspiciatis exercitationem | ||
consectetur minus illum. Quos quaerat omnis aut eius eos dolores velit. Et quia vel ea unde eum repudiandae. Repellat et | ||
ab sed. | ||
</text> | ||
<text style="text"> | ||
Velit voluptas et alias atque provident sapiente consequuntur deserunt. Dolorem et | ||
non error dolorem voluptate amet accusantium. Corporis rerum sed labore quae sed qui quis quasi. Illo pariatur sint qui. | ||
Quasi quaerat id molestias. Necessitatibus et ipsa quia asperiores laborum neque. Quisquam dolorem consequatur illum. Ut | ||
magni iusto explicabo blanditiis quasi laborum incidunt earum. Eius ut in rerum ipsam. Officiis dolores suscipit | ||
consequatur placeat commodi eum. Vel possimus placeat aut eos tempore saepe. Esse assumenda eum illo sed aut earum quia | ||
voluptatibus. Recusandae qui iusto corporis sed atque. Veniam et possimus praesentium. Cum molestiae non velit minus | ||
voluptatibus quos illo sed. Et omnis ut soluta qui inventore molestias. Dolores voluptatem perspiciatis exercitationem | ||
consectetur minus illum. Quos quaerat omnis aut eius eos dolores velit. Et quia vel ea unde eum repudiandae. Repellat et | ||
ab sed. | ||
</text> | ||
<text style="text"> | ||
Velit voluptas et alias atque provident sapiente consequuntur deserunt. Dolorem et | ||
non error dolorem voluptate amet accusantium. Corporis rerum sed labore quae sed qui quis quasi. Illo pariatur sint qui. | ||
Quasi quaerat id molestias. Necessitatibus et ipsa quia asperiores laborum neque. Quisquam dolorem consequatur illum. Ut | ||
magni iusto explicabo blanditiis quasi laborum incidunt earum. Eius ut in rerum ipsam. Officiis dolores suscipit | ||
consequatur placeat commodi eum. Vel possimus placeat aut eos tempore saepe. Esse assumenda eum illo sed aut earum quia | ||
voluptatibus. Recusandae qui iusto corporis sed atque. Veniam et possimus praesentium. Cum molestiae non velit minus | ||
voluptatibus quos illo sed. Et omnis ut soluta qui inventore molestias. Dolores voluptatem perspiciatis exercitationem | ||
consectetur minus illum. Quos quaerat omnis aut eius eos dolores velit. Et quia vel ea unde eum repudiandae. Repellat et | ||
ab sed. | ||
</text> | ||
</view> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<xs:schema | ||
xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
attributeFormDefault="qualified" | ||
elementFormDefault="qualified" | ||
targetNamespace="https://hyperview.org/scroll-opacity" | ||
xmlns:hv="https://hyperview.org/hyperview" | ||
> | ||
<xs:import | ||
namespace="https://hyperview.org/hyperview" | ||
schemaLocation="hyperview.xsd" | ||
/> | ||
<xs:element name="scroll-opacity"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:any minOccurs="0" maxOccurs="unbounded" /> | ||
</xs:sequence> | ||
<xs:attribute name="context-key" type="xs:token" /> | ||
<xs:attribute name="axis"> | ||
<xs:simpleType> | ||
<xs:restriction base="xs:string"> | ||
<xs:enumeration value="horizontal" /> | ||
<xs:enumeration value="vertical" /> | ||
</xs:restriction> | ||
</xs:simpleType> | ||
</xs:attribute> | ||
<xs:attribute name="distance" type="xs:nonNegativeInteger" /> | ||
<xs:attribute name="duration" type="xs:nonNegativeInteger" /> | ||
<xs:attribute name="initial-opacity" type="xs:nonNegativeInteger" /> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { HvComponentProps, LocalName } from 'hyperview'; | ||
import Hyperview, { useScrollContext } from 'hyperview'; | ||
import React, { useEffect, useRef } from 'react'; | ||
import { getNumberAttr, namespaceURI } from './utils'; | ||
import { Animated } from 'react-native'; | ||
|
||
const ScrollOpacity = (props: HvComponentProps) => { | ||
const contextKey = props.element.getAttributeNS(namespaceURI, 'context-key'); | ||
const axis = props.element.getAttributeNS(namespaceURI, 'axis') || 'vertical'; | ||
const distance = getNumberAttr(props.element, 'distance', 0); | ||
const duration = getNumberAttr(props.element, 'duration', 0); | ||
const initialOpacity = getNumberAttr(props.element, 'initial-opacity', 1); | ||
const opacity = useRef(new Animated.Value(initialOpacity)).current; | ||
const { offsets } = useScrollContext(); | ||
|
||
const defaultPosition = { x: 0, y: 0 }; | ||
const { x, y } = (() => { | ||
if (!contextKey) { | ||
return defaultPosition; | ||
} | ||
if (!offsets[contextKey]) { | ||
return defaultPosition; | ||
} | ||
return offsets[contextKey]; | ||
})(); | ||
const position = (() => { | ||
if (axis === 'horizontal') { | ||
return x; | ||
} | ||
if (axis === 'vertical') { | ||
return y; | ||
} | ||
throw new Error(`Invalid axis: ${axis}`); | ||
})(); | ||
|
||
useEffect(() => { | ||
const toValue = Math.min( | ||
Math.max(position / Math.max(distance, 2) - 1, 0), | ||
1, | ||
); | ||
Animated.timing(opacity, { | ||
duration, | ||
toValue, | ||
useNativeDriver: true, | ||
}).start(); | ||
}, [axis, contextKey, distance, duration, opacity, position, x, y]); | ||
|
||
const children = (Hyperview.renderChildren( | ||
props.element, | ||
props.stylesheets, | ||
props.onUpdate, | ||
props.options, | ||
) as unknown) as JSX.Element; | ||
return <Animated.View style={{ opacity }}>{children}</Animated.View>; | ||
}; | ||
|
||
ScrollOpacity.namespaceURI = namespaceURI; | ||
ScrollOpacity.localName = 'scroll-opacity' as LocalName; | ||
ScrollOpacity.localNameAliases = [] as LocalName[]; | ||
|
||
export { ScrollOpacity }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ScrollOpacity } from './ScrollOpacity'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const namespaceURI = 'https://hyperview.org/scroll-opacity'; | ||
|
||
export const getNumberAttr = ( | ||
element: Element, | ||
attrName: string, | ||
defaultValue: number | null = null, | ||
): number => { | ||
const value: string | null = element.getAttributeNS(namespaceURI, attrName); | ||
if (!value) { | ||
return defaultValue || 0; | ||
} | ||
return parseInt(value, 10); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.