2.0.46 • Published 3 months ago

fragmented v2.0.46

Weekly downloads
3
License
MIT, Anti-996
Repository
github
Last release
3 months ago

Fragmented

Relates to URL hash fragments.

Fragmented allows your application to affect and be affected by the hash fragment of the URL in the browser's address bar. Changes to the fragment are invisible to the server whilst being included in the browser's history. For this reason fragments are useful to single page applications that need the back and forward buttons to work for them, and for deep linking.

All that Fragmented does is create a global fragment variable, the value of which the application can get or set, and which also has onFragmentChange and offFragmentChange properties so that handlers can be registered and unregistered.

Installation

With npm:

npm install fragmented

You can also clone the repository with Git...

git clone https://github.com/djalbat/fragmented.git

...then install the dependencies with npm from within the project's root directory:

npm install

You will need to do this if you want to look at the example.

Example

There is a small development server that can be run from within the project's directory with the following command:

npm start

The example will then be available at the following URL:

http://localhost:8888

The source for the example can be found in the src/example.js file and corresponding src/example folder. You are encouraged to try the example whilst reading what follows. You can rebuild it on the fly with the following command:

npm run watch-debug

The development server will reload the page whenever you make changes.

One last thing to bear in mind is that this package is included by way of a relative rather than a package import. If you are importing it into your own application, however, you should use the standard package import.

Usage

There are no objects exported as such, you only need to import the package:

import "fragmented";

To assign the current value of the fragment to a variable:

const pageFragment = fragment;

To set the fragment:

fragment = "test";

Registering and un-registering change handlers:

const { onFragmentChange, offFragmentChange } = fragment;

function fragmentChangeHandler() {
  console.log(fragment)
}

onFragmentChange(fragmentChangeHandler);

// offFragmentChange(fragmentChangeHandler);

Note that change handlers are invoked whenever the fragment variable is set. If you do not want this behaviour, in other words you want to be able to change the fragment in the address bar without your change handlers being invoked, you can to this with an additional setFragment() function as follows:

const { setFragment } = fragment;

setFragment("test"); // change handlers won't be invoked

Also note that the fragment variable is an instance of the String object, rather than a string primitive, in order that properties on it can be defined. So use == rather than === if equating it to a string primitive. Finally, note that if you use a switch statement, which uses strict equality, you will need to explicitly coerce the fragment variable to a string primitive before passing it in.

Finally, if you do not like the thought of a global fragment variable, you can of course refer to it as window.fragment.

Building

Automation is thanks to npm scripts, have a look at the package.json file. The pertinent commands are:

npm run build-debug
npm run watch-debug

Contact

  • james.smith@djalbat.com
2.0.46

3 months ago

2.0.44

4 months ago

2.0.45

4 months ago

2.0.42

5 months ago

2.0.43

5 months ago

2.0.40

8 months ago

2.0.41

8 months ago

2.0.39

1 year ago

2.0.37

2 years ago

2.0.35

2 years ago

2.0.29

2 years ago

2.0.33

2 years ago

2.0.34

2 years ago

2.0.31

2 years ago

2.0.32

2 years ago

2.0.30

2 years ago

2.0.28

2 years ago

2.0.26

2 years ago

2.0.27

2 years ago

2.0.25

3 years ago

2.0.24

3 years ago

2.0.23

3 years ago

2.0.22

3 years ago

2.0.21

3 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.18

4 years ago

2.0.17

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.8

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.2

7 years ago