@unsetsoft/ryunixjs 0.2.8 → 0.2.9-nightly.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/dom.js CHANGED
@@ -513,9 +513,61 @@ function useEffect(effect, deps) {
513
513
  hookIndex++;
514
514
  }
515
515
 
516
+ /**
517
+ * The Router component is responsible for rendering a specific component based on the current path in
518
+ * the browser's URL.
519
+ * @returns The `Router` component returns the `component` if the `currentPath` matches the specified
520
+ * `path`, otherwise it returns `null`.
521
+ */
522
+ const Router = ({ path, component }) => {
523
+ const [currentPath, setCurrentPath] = useStore(window.location.pathname);
524
+
525
+ useEffect(() => {
526
+ const onLocationChange = () => {
527
+ setCurrentPath(() => window.location.pathname);
528
+ };
529
+
530
+ window.addEventListener("navigate", onLocationChange);
531
+ window.addEventListener("pushsatate", onLocationChange);
532
+ window.addEventListener("popstate", onLocationChange);
533
+
534
+ return () => {
535
+ window.removeEventListener("navigate", onLocationChange);
536
+ window.removeEventListener("pushsatate", onLocationChange);
537
+ window.removeEventListener("popstate", onLocationChange);
538
+ };
539
+ }, [currentPath]);
540
+
541
+ return currentPath === path ? component() : null;
542
+ };
543
+
544
+ /**
545
+ * The Link function is a Ryunix component that prevents page reload when a link is clicked and updates
546
+ * the browser's history and local storage.
547
+ * @param props - The `props` parameter is an object that contains the properties passed to the `Link`
548
+ * component. These properties can include the following:
549
+ * @returns The Link component is being returned.
550
+ */
551
+ const Link = (props) => {
552
+ const preventReload = (event) => {
553
+ event.preventDefault();
554
+ if (window.location.pathname !== props.to) {
555
+ window.history.pushState({}, "", props.to);
556
+ const navigationEvent = new Event("pushsatate");
557
+ window.dispatchEvent(navigationEvent);
558
+ localStorage.setItem("pathname", props.to);
559
+ }
560
+ };
561
+ return (
562
+ <a href={props.to} onClick={preventReload} {...props}>
563
+ {props.children}
564
+ </a>
565
+ );
566
+ };
567
+
516
568
  // export
517
569
 
518
- export { useStore, useEffect };
570
+ export { useStore, useEffect, Router, Link };
519
571
 
520
572
  export default {
521
573
  createElement,
package/lib/main.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import Ryunix from "./dom";
2
2
 
3
- export { useStore, useEffect } from "./dom";
3
+ export { useStore, useEffect, Router, Link } from "./dom";
4
4
 
5
5
  window.Ryunix = Ryunix;
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unsetsoft/ryunixjs",
3
- "version": "0.2.8",
3
+ "version": "0.2.9-nightly.1",
4
4
  "license": "MIT",
5
5
  "main": "./dist/Ryunix.js",
6
6
  "private": false,
package/webpack.config.js CHANGED
@@ -14,7 +14,10 @@ module.exports = {
14
14
  filename: "./assets/js/[chunkhash].bundle.js",
15
15
  devtoolModuleFilenameTemplate: "ryunix/[resource-path]",
16
16
  },
17
-
17
+ devServer: {
18
+ port: 3000,
19
+ historyApiFallback: { index: "/", disableDotRule: true },
20
+ },
18
21
  module: {
19
22
  rules: [
20
23
  {