diff --git a/demo/backend/behaviors/basic/triggers/unload/index.xml.njk b/demo/backend/behaviors/basic/triggers/unload/index.xml.njk new file mode 100644 index 000000000..1793609b6 --- /dev/null +++ b/demo/backend/behaviors/basic/triggers/unload/index.xml.njk @@ -0,0 +1,30 @@ +--- +permalink: "/behaviors/basic/unload/index.xml" +tags: "Behaviors/Basic/Triggers" +hv_title: "Unload" +hv_button_behavior: "back" +--- + +{% extends 'templates/scrollview.xml.njk' %} +{% from 'macros/button/index.xml.njk' import button %} +{% from 'macros/description/index.xml.njk' import description %} + +{% block content %} + {{ description('Navigate to the next screen. When you navigate back, a message will be displayed.') }} + + + The screen was unloaded + + + {% call button('Next', attributes={xmlns:"https://hyperview.org/hyperview"}) -%} + + {%- endcall %} + + + +{% endblock %} diff --git a/demo/backend/behaviors/basic/triggers/unload/unload.xml.njk b/demo/backend/behaviors/basic/triggers/unload/unload.xml.njk new file mode 100644 index 000000000..794ef99a5 --- /dev/null +++ b/demo/backend/behaviors/basic/triggers/unload/unload.xml.njk @@ -0,0 +1,22 @@ +--- +permalink: "/behaviors/basic/triggers/unload/unload.xml" +--- + +{% extends 'templates/scrollview.xml.njk' %} +{% from 'macros/button/index.xml.njk' import button %} +{% from 'macros/description/index.xml.njk' import description %} + +{% block content %} + {{ description('Navigate back to the previous screen to see the confirmation message.') }} + + {% call button('Go back', attributes={xmlns:"https://hyperview.org/hyperview"}) -%} + + {%- endcall %} + + + +{% endblock %} diff --git a/docs/reference_behavior_attributes.md b/docs/reference_behavior_attributes.md index 0e93a589b..1218cbb20 100644 --- a/docs/reference_behavior_attributes.md +++ b/docs/reference_behavior_attributes.md @@ -105,6 +105,16 @@ These elements support the `load` trigger: - [``](/docs/reference_text) - [``](/docs/reference_image) +### `unload` + +Triggers when the element is unloaded in the screen. + +> Elements with `hide="true"` are not unloaded in the screen. This means any hidden element (or child element) with the `unload` trigger will not be executed. + +These elements support the `unload` trigger: + +- [``](/docs/reference_view) + ### `select` Triggers when the element is selected. Only works on selectable elements. diff --git a/schema/core.xsd b/schema/core.xsd index 94e8a239c..c8c0b8a3f 100644 --- a/schema/core.xsd +++ b/schema/core.xsd @@ -42,6 +42,7 @@ + diff --git a/src/core/hyper-ref/index.tsx b/src/core/hyper-ref/index.tsx index a272b1996..417baadc4 100644 --- a/src/core/hyper-ref/index.tsx +++ b/src/core/hyper-ref/index.tsx @@ -100,6 +100,9 @@ export default class HyperRef extends PureComponent { Events.unsubscribe(this.onEventDispatch); // Deregister event listener for back triggers this.removeBackBehaviors(); + if (this.props.element.localName === LOCAL_NAME.VIEW) { + this.triggerUnLoadBehaviors(); + } } updateBehaviorElements = () => { @@ -218,6 +221,27 @@ export default class HyperRef extends PureComponent { } }; + triggerUnLoadBehaviors = () => { + let unloadBehaviors = this.getBehaviorElements(TRIGGERS.UNLOAD); + if ( + this.props.options?.staleHeaderType === + X_RESPONSE_STALE_REASON.STALE_IF_ERROR + ) { + const loadStaleBehaviors = this.getBehaviorElements( + TRIGGERS.LOAD_STALE_ERROR, + ); + unloadBehaviors = unloadBehaviors.concat(loadStaleBehaviors); + } + + if (unloadBehaviors.length > 0) { + Behaviors.triggerBehaviors( + this.props.element, + unloadBehaviors, + this.props.onUpdate, + ); + } + }; + TouchableView = ({ children }: { children: JSX.Element }): JSX.Element => { const behaviors = this.behaviorElements.filter( e => diff --git a/src/types.ts b/src/types.ts index 800b837fe..0289ba3cd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -276,6 +276,7 @@ export const TRIGGERS = Object.freeze({ PRESS_OUT: 'pressOut', REFRESH: 'refresh', SELECT: 'select', + UNLOAD: 'unload', VISIBLE: 'visible', });