0.0.11 • Published 2 years ago

web_page_state v0.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Page State Helper for client side websites

Utility hooks to help organize a site's client-side state, and easily change the view according to state through html data attributes. Conceptually combines ideas from Bootstrap & Redux.

See published npm package

Import:

  • Web: <script src="https://unpkg.com/page_state/build/bundle.min.js"></script>
  • Node: npm i web_page_state

How to use?

- HTML

AttributeNoteExample
data-ws-show-ifShow element if ANY (ie OR) of the state props specified in the attribute (space separated) is true, otherwise set 'display' style to 'none'. To achieve AND simply nest elements.- Simple: <button data-ws-show-if="isSignedIn">Sign Out</button> - OR: <div data-ws-show-if="isIOS isAndroid">You are on iOS OR Android</div> - AND: <div data-ws-show-if="isSignedIn"><p data-ws-show-if="isIOS"><Welcome user on iOS</p></div>
data-ws-show-if-notShow element if ANY (ie OR) of the NEGATED state props specified in the attribute (space separated) is true, otherwise set 'display' style to 'none'<button data-ws-show-if-not="isSignedIn">Sign In</button>
data-ws-labelReplaces the element's innerText with the prop's value<p>Welcome <span data-ws-label="user.name"></span></p>

- js

PropertyTypeDescription
pageStateObjectHolds the raw state.
updatePageStateWithParamsFunctionExamples: updatePageStateWithParams({countryName: "Brazil"})
toastNotificationFunctionExample: toastNotification("Copied", "Successfully copied to clipboard", false, false);
dismissNotificationFunctionRemove previously toasted notification
setUserFunctionExamples:- On sign in: setUser({name: "Ronen", uid: "987dkjbc987"}); - On sign out: setUser(null);
getParamFunctiongetParam("user.uid");
toggleParamFunctiontoggleParam("isFullScreen");
registerListenerFunctionReturns the index / id as number of the new listener. This index can be used to remove the listener. Example: let index = PageState.registerListener(function(newState){/* do something */}); PageState.unregisterListener(index);
unregisterListenerFunctionUse the index generated when 'registered'. Example: let index = PageState.registerListener(function(newState){/* do something */}); PageState.unregisterListener(index);

- State's special keys - do not set directly

// TODO: Prohibit direct setting by mistake?

KeyType
userObject, set with setUser
isNotificationboolean, semi-automatic: based on 'toastNotification'
notificationTitleboolean, semi-automatic: based on 'toastNotification'
notificationMsgboolean, semi-automatic: based on 'toastNotification'
platformString, automatic
isMobileboolean, automatic
isIOSboolean, automatic
isAndroidboolean, automatic
isOfflineboolean, automatic
isSignedInboolean, semi-automatic: based on 'user'

How to use?

When used in browser:

Place the following at the end of the body tag, and prior to using the wsGlobals.PageState.updateStateWithProps

<script src="https://unpkg.com/page_state/build/bundle.min.js"></script>

No need to initialize - it is initialized in code.

When used in a webpack / other node client-side webpage builder:

Use import or require... // TODO: Do we need to initialize? Check...

Showcases

This is used on the following sites:

TODOs:

  • In data-ws-show-if-not we let the function - if exists - override the property, which is inconsistent with the non-negated data-ws-show-if...
  • Prohibit direct setting of reserved props / functions by mistake?
  • Set/get style to the element's html set style prior to setting it to "none"

How to contribute & publish updates to this package?

  • This package is published both on GitHub and on npmjs.
  • To publish an updated version - simply run npm publish, it will commit & push updates both to github and npm.