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

Repair stencil-route-prompt (ask user for confirmation before navigation) #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stencil/router",
"version": "1.0.1",
"version": "1.0.2",
"description": "Stencil Router",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
42 changes: 24 additions & 18 deletions packages/router/src/components/prompt/prompt.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Component, Prop, Element, ComponentInterface, Watch, State } from '@stencil/core';
import { RouterHistory, Prompt } from '../../global/interfaces';
import ActiveRouter from '../../global/active-router';
import {
Component,
Prop,
Element,
ComponentInterface,
Watch,
State,
} from "@stencil/core";
import { RouterHistory, Prompt } from "../../global/interfaces";
import ActiveRouter from "../../global/active-router";

@Component({
tag: 'stencil-router-prompt'
tag: "stencil-router-prompt",
})
export class StencilRouterPrompt implements ComponentInterface {
@Element() el!: HTMLElement;
@Element() el!: HTMLStencilRouterPromptElement;
@Prop() when = true;
@Prop() message: string | Prompt = '';
@Prop() message: string | Prompt = "";
@Prop() history?: RouterHistory;

@State() unblock?: () => void;
Expand All @@ -30,20 +37,21 @@ export class StencilRouterPrompt implements ComponentInterface {
}

componentWillLoad() {
if (this.when ) {
if (this.when) {
this.enable(this.message);
}
}

@Watch('message')
@Watch('when')
updateMessage(newMessage: string, prevMessage: string) {
if (this.when) {
if (!this.when || prevMessage !== newMessage) {
this.enable(this.message);
}
} else {
updateMessage(newMessage: string, prevMessage: string): void; // `message` updated
updateMessage(newMessage: boolean, prevMessage: boolean): void; // `when` updated

@Watch("message")
@Watch("when")
updateMessage<T extends string | boolean>(newMessage: T, prevMessage: T) {
if (!this.when) {
this.disable();
} else if (prevMessage !== newMessage) {
this.enable(this.message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to simplify the logic here a little while being more earnest about the parameter types.

}
}

Expand All @@ -56,6 +64,4 @@ export class StencilRouterPrompt implements ComponentInterface {
}
}

ActiveRouter.injectProps(StencilRouterPrompt, [
'history',
]);
ActiveRouter.injectProps(StencilRouterPrompt, ["history"]);
11 changes: 5 additions & 6 deletions packages/router/src/utils/createBrowserHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const createBrowserHistory = (win: Window, props: CreateBrowserHistoryOptions =
const scrollHistory = createScrollHistory(win);

const forceRefresh = (props.forceRefresh != null) ? props.forceRefresh : false;
const getUserConfirmation = (props.getUserConfirmation != null) ? props.getUserConfirmation : getConfirmation;
const getUserConfirmation =
props.getUserConfirmation != null
? props.getUserConfirmation
: getConfirmation.bind(undefined, win);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure window is passed as first parameter to getConfirmation.

const keyLength = (props.keyLength != null) ? props.keyLength : 6;
const basename = props.basename ? stripTrailingSlash(addLeadingSlash(props.basename)) : '';

Expand Down Expand Up @@ -83,11 +86,9 @@ const createBrowserHistory = (win: Window, props: CreateBrowserHistoryOptions =
return createLocation(path, state, key || createKey(keyLength));
};


const transitionManager = createTransitionManager();

const setState = (nextState?: NextState) => {

// Capture location for the view before changing history.
scrollHistory.capture(history.location.key);

Expand All @@ -103,7 +104,7 @@ const createBrowserHistory = (win: Window, props: CreateBrowserHistoryOptions =
);
};

const handlePopState = (event: any) => {
const handlePopState = (event: PopStateEvent) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misc. clean up discovered while reading through code. Seemed like a specific type would be better.

// Ignore extraneous popstate events in WebKit.
if (!isExtraneousPopstateEvent(globalNavigator, event)) {
handlePop(getDOMLocation(event.state));
Expand All @@ -114,7 +115,6 @@ const createBrowserHistory = (win: Window, props: CreateBrowserHistoryOptions =
handlePop(getDOMLocation(getHistoryState()));
};


const handlePop = (location: LocationSegments) => {
if (forceNextPop) {
forceNextPop = false;
Expand Down Expand Up @@ -263,7 +263,6 @@ const createBrowserHistory = (win: Window, props: CreateBrowserHistoryOptions =
const goBack = () => go(-1);
const goForward = () => go(1);


const checkDOMListeners = (delta: number) => {
listenerCount += delta;

Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/utils/createHashHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const createHashHistory = (win: Window, props: CreateHashHistoryOptions = {}) =>
const keyLength = (props.keyLength != null) ? props.keyLength : 6;

const {
getUserConfirmation = getConfirmation,
getUserConfirmation = getConfirmation.bind(undefined, win),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure window is passed as first parameter to getConfirmation.

hashType = 'slash'
} = props;
const basename = props.basename ? stripTrailingSlash(addLeadingSlash(props.basename)) : '';
Expand Down