Skip to content

Commit

Permalink
Add support of a new behaviour for Add to home screen banner. Fix #52…
Browse files Browse the repository at this point in the history
…8 (#529)

Starting from Chrome 68 "Add to home screen" banner is not showed automatically, a user needs to trigger this event manually.

<img src="https://user-images.githubusercontent.com/2954281/47683046-0b5e9680-dbd7-11e8-9a82-5e2593d924c8.jpg" width="200">
  • Loading branch information
ozasadnyy authored Oct 31, 2018
1 parent eaccec2 commit aaeb300
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
3 changes: 3 additions & 0 deletions data/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,8 @@
"tags": "Tags",
"complexity": "Complexity",
"clear": "Clear all"
},
"addToHomeScreen": {
"cta": "Add to Home Screen"
}
}
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@
ga('send', 'timing', 'index.html', 'render', indexRenderTime);
}
};

window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
uiActions.setAddToHomeScreen(e);
});
</script>

</body>
Expand Down
6 changes: 6 additions & 0 deletions scripts/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const uiActions = {
value,
});
},
setAddToHomeScreen: (value) => {
store.dispatch({
type: SET_ADD_TO_HOMESCREEN,
value,
});
},
};

const routingActions = {
Expand Down
1 change: 1 addition & 0 deletions scripts/redux/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const TOGGLE_DRAWER = 'app/Drawer/TOGGLE_DRAWER';
const SET_VIEWPORT_SIZE = 'app/Viewport/SET_VIEWPORT_SIZE';
const SET_HERO_SETTINGS = 'app/Hero/SET_HERO_SETTINGS';
const SET_ADD_TO_HOMESCREEN = 'app/APP/SET_ADD_TO_HOMESCREEN';
// router
const SET_ROUTE = 'app/Routing/SET_ROUTE';
const SET_SUB_ROUTE = 'app/Routing/SET_SUB_ROUTE';
Expand Down
4 changes: 4 additions & 0 deletions scripts/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const uiReducer = (state = initialState.ui, action) => {
return Object.assign({}, state, {
heroSettings: Object.assign({}, state.heroSettings, action.value),
});
case SET_ADD_TO_HOMESCREEN:
return Object.assign({}, state, {
addToHomescreen: action.value,
});
default:
return state;
}
Expand Down
5 changes: 5 additions & 0 deletions src/elements/header-toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
type: Object,
statePath: 'ui.viewport',
},
addToHomescreen: {
type: Object,
statePath: 'ui.addToHomescreen',
},
heroSettings: {
type: Object,
statePath: 'ui.heroSettings',
Expand All @@ -267,6 +271,7 @@
type: Object,
statePath: 'tickets',
},
transparent: Boolean,
};
}

Expand Down
65 changes: 51 additions & 14 deletions src/hoverboard-app.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@
margin-left: 6px;
}

.buy-ticket {
.bottom-drawer-link {
padding: 16px 24px;
cursor: pointer;
}

@media (min-width: 640px) {
Expand Down Expand Up @@ -163,19 +164,30 @@ <h3 class="location">{$ location.short $}</h3>
{% endfor %}
</iron-selector>

<a
class="buy-ticket"
href$="[[tickets.url]]"
target="_blank"
rel="noopener noreferrer"
ga-on="click"
ga-event-category="ticket button"
ga-event-action="buy_click"
layout horizontal center
>
<span>{$ buyTicket $}</span>
<iron-icon icon="hoverboard:open-in-new"></iron-icon>
</a>
<div>
<a
class="bottom-drawer-link"
on-tap="_onAddToHomescreen"
hidden$="[[_isAddToHomeScreenHidden(ui.addToHomescreen, viewport.isLaptopPlus)]]"
>
{$ addToHomeScreen.cta $}
</a>

<a
class="bottom-drawer-link"
href$="[[_getTicketUrl(tickets)]]"
target="_blank"
rel="noopener noreferrer"
on-tap="closeDrawer"
ga-on="click"
ga-event-category="ticket button"
ga-event-action="buy_click"
layout horizontal center
>
<span>{$ buyTicket $}</span>
<iron-icon icon="hoverboard:open-in-new"></iron-icon>
</a>
</div>
</div>

</app-drawer>
Expand Down Expand Up @@ -435,6 +447,31 @@ <h3 class="location">{$ location.short $}</h3>
_toggleDrawer(e) {
uiActions.toggleDrawer(e.detail.value);
}

_getTicketUrl(tickets) {
if (!tickets.list.length) return '';
const availableTicket = tickets.list.filter((ticket) => ticket.available)[0];
return availableTicket ? availableTicket.url : tickets.list[0].url;
}

_isAddToHomeScreenHidden(addToHomescreen, isTabletPlus) {
return isTabletPlus || !addToHomescreen;
}

_onAddToHomescreen() {
if (!this.ui.addToHomescreen) this.closeDrawer();
this.ui.addToHomescreen.prompt();
this.ui.addToHomescreen.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
ga('send', 'event', 'add_to_home_screen_prompt', 'accepted');
} else {
ga('send', 'event', 'add_to_home_screen_prompt', 'dismissed');
}
uiActions.setAddToHomeScreen(null);
this.closeDrawer();
});
}
}

window.customElements.define(HoverboardApp.is, HoverboardApp);
Expand Down
1 change: 1 addition & 0 deletions src/mixins/redux-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
title: '',
},
heroSettings: {},
addToHomescreen: null,
},
routing: {
route: 'home',
Expand Down

0 comments on commit aaeb300

Please sign in to comment.