-
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.
feat(demo):
nav:back
element (#1110)
Add new element to render the back or close button conditionally, based on the screen's navigation context. https://github.com/user-attachments/assets/c53d1c2b-16fe-4ac1-a515-85a030ec6e09 --------- Co-authored-by: flochtililoch <flochtililoch@gmail.com> Co-authored-by: Hardin Gray <hgray@instawork.com>
- Loading branch information
1 parent
7255fa7
commit fb13837
Showing
10 changed files
with
186 additions
and
46 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
demo/backend/advanced/community/elements/nav-back/_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" | ||
/> |
26 changes: 26 additions & 0 deletions
26
demo/backend/advanced/community/elements/nav-back/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,26 @@ | ||
--- | ||
permalink: "/backend/advanced/community/nav-back/index.xml" | ||
tags: "Advanced/Community/Elements" | ||
hv_button_behavior: "back" | ||
hv_title: "Nav Back" | ||
--- | ||
|
||
{% from 'macros/button/index.xml.njk' import button %} | ||
{% from 'macros/description/index.xml.njk' import description %} | ||
{% extends 'templates/scrollview.xml.njk' %} | ||
|
||
{% block content %} | ||
{{ description('Tapping one of the buttons will render the same screen with a different navigation style. The control to navigate back will be dynamic, based on the navigation context.') }} | ||
{% call button('Push') -%} | ||
<behavior | ||
action="push" | ||
href="/hyperview/public/advanced/community/elements/nav-back/screen.xml" | ||
/> | ||
{%- endcall %} | ||
{% call button('New') -%} | ||
<behavior | ||
action="new" | ||
href="/hyperview/public/advanced/community/elements/nav-back/screen.xml" | ||
/> | ||
{%- endcall %} | ||
{% endblock %} |
44 changes: 44 additions & 0 deletions
44
demo/backend/advanced/community/elements/nav-back/screen.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,44 @@ | ||
--- | ||
permalink: "/backend/advanced/community/nav-back/screen.xml" | ||
hv_title: "Nav Back" | ||
--- | ||
|
||
{% from 'macros/button/index.xml.njk' import button %} | ||
{% from 'macros/description/index.xml.njk' import description %} | ||
{% extends 'templates/base.xml.njk' %} | ||
|
||
{% block styles %} | ||
{% include './_styles.xml.njk' %} | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<header style="header"> | ||
<nav:back xmlns:nav="https://hyperview.org/navigation"> | ||
<view | ||
nav:role="close" | ||
action="close" | ||
href="#" | ||
style="header-btn" | ||
> | ||
{% include 'icons/close.svg' %} | ||
</view> | ||
<view | ||
nav:role="back" | ||
action="back" | ||
href="#" | ||
style="header-btn" | ||
> | ||
{% include 'icons/back.svg' %} | ||
</view> | ||
</nav:back> | ||
<text style="header-title">{{ hv_title }}</text> | ||
</header> | ||
<text style="text"> | ||
The button to navigate back to the previous screen will be rendered dynamically, | ||
based on which kind of navigator the screen is currently loaded on. For regular screens | ||
(added to the stack via "push" action), a back-arrow button will be rendered. For | ||
modal screens (added to the stack via "new" action), a close button will be rendered. | ||
Also (not demo'd here), if the screen is the first screen of the stack and there is no | ||
place to navigate back to, no button will be rendered. | ||
</text> | ||
{% 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
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,40 @@ | ||
import type { HvComponentProps, LocalName } from 'hyperview'; | ||
import Hyperview from 'hyperview'; | ||
import { NavigationContext } from '@react-navigation/native'; | ||
import { findElements } from '../../Helpers'; | ||
import { useContext } from 'react'; | ||
|
||
export const namespaceURI = 'https://hyperview.org/navigation'; | ||
|
||
const NavBack = (props: HvComponentProps) => { | ||
const ctx = useContext(NavigationContext); | ||
const state = ctx?.getState(); | ||
const route = state?.routes[state.index]; | ||
|
||
// Screen is first on the stack? Don't render anything | ||
if (state?.index === 0) { | ||
return null; | ||
} | ||
|
||
const role = route?.name === 'modal' ? 'close' : 'back'; | ||
const [element] = findElements(namespaceURI, props.element, ['role']).filter( | ||
el => { | ||
return el.getAttributeNS(namespaceURI, 'role') === role; | ||
}, | ||
); | ||
if (!element) { | ||
return null; | ||
} | ||
return (Hyperview.renderElement( | ||
element, | ||
props.stylesheets, | ||
props.onUpdate, | ||
props.options, | ||
) as unknown) as JSX.Element; | ||
}; | ||
|
||
NavBack.namespaceURI = namespaceURI; | ||
NavBack.localName = 'back' as LocalName; | ||
NavBack.localNameAliases = [] as LocalName[]; | ||
|
||
export { NavBack }; |
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 { NavBack } from './NavBack'; |
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