@react-foundry/anchor 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2019-2025 Crown Copyright
4
+ Copyright (C) 2019-2026 Daniel A.C. Martin
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ this software and associated documentation files (the "Software"), to deal in
8
+ the Software without restriction, including without limitation the rights to
9
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
+ of the Software, and to permit persons to whom the Software is furnished to do
11
+ so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ React Foundry - Anchor
2
+ ======================
3
+
4
+ A drop in replacement for the HTML `<a>` tag that works with [React Router].
5
+
6
+
7
+ Preview
8
+ -------
9
+
10
+ ![Preview][Preview]
11
+
12
+
13
+ Using this package
14
+ ------------------
15
+
16
+ First install the package into your project:
17
+
18
+ ```shell
19
+ npm install -S @react-foundry/anchor
20
+ ```
21
+
22
+ Then use it in your code as follows:
23
+
24
+ ```js
25
+ import React, { createElement as h } from 'react';
26
+ import { A } from '@react-foundry/anchor';
27
+
28
+ export const MyComponent = props => (
29
+ <A href="#">My link</A>
30
+ );
31
+
32
+ export default MyComponent;
33
+ ```
34
+
35
+
36
+ Working on this package
37
+ -----------------------
38
+
39
+ Before working on this package you must install its dependencies using
40
+ the following command:
41
+
42
+ ```shell
43
+ pnpm install
44
+ ```
45
+
46
+
47
+ ### Testing
48
+
49
+ ```shell
50
+ npm test
51
+ ```
52
+
53
+
54
+ ### Building
55
+
56
+ ```shell
57
+ npm run build
58
+ ```
59
+
60
+
61
+ ### Clean-up
62
+
63
+ ```shell
64
+ npm run clean
65
+ ```
66
+
67
+
68
+ [React Router]: https://reacttraining.com/react-router/
69
+ [Preview]: ../../__image_snapshots__/storyshots-itest-ts-image-storyshots-components-anchor-standard-1-snap.png
File without changes
@@ -0,0 +1,11 @@
1
+ import { AnchorHTMLAttributes, FC, ReactNode } from 'react';
2
+ import { StandardProps } from '@react-foundry/component-helpers';
3
+ import '../assets/Anchor.scss';
4
+ export type AnchorProps = StandardProps & AnchorHTMLAttributes<HTMLAnchorElement> & {
5
+ children?: ReactNode;
6
+ /** Whether to force the link to be treated as external (useful for internal links that are NOT handled by the application) */
7
+ forceExternal?: boolean;
8
+ };
9
+ export declare const Anchor: FC<AnchorProps>;
10
+ export default Anchor;
11
+ export declare const A: FC<AnchorProps>;
package/dist/Anchor.js ADDED
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ 'use client';
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.A = exports.Anchor = void 0;
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ const react_1 = require("react");
7
+ const client_component_helpers_1 = require("@react-foundry/client-component-helpers");
8
+ const component_helpers_1 = require("@react-foundry/component-helpers");
9
+ const router_1 = require("@react-foundry/router");
10
+ const uri_1 = require("@react-foundry/uri");
11
+ require("../assets/Anchor.scss");
12
+ const supportedProtocols = [
13
+ 'http:',
14
+ 'https:'
15
+ ];
16
+ const AnchorInner = ({ children, classBlock, classModifiers: _classModifiers = [], className, forceExternal = false, href, ...attrs }) => {
17
+ const isMounted = (0, client_component_helpers_1.useIsMounted)();
18
+ const active = (0, router_1.useIsActive)()(href || '');
19
+ const current = (0, router_1.useLocation)();
20
+ const classModifiers = [
21
+ active ? 'active' : '',
22
+ ...(Array.isArray(_classModifiers) ? _classModifiers : [_classModifiers])
23
+ ];
24
+ const classes = (0, component_helpers_1.classBuilder)('penultimate-anchor', classBlock, classModifiers, className);
25
+ const url = uri_1.URI.parse(href || '');
26
+ const unsupported = url.protocol !== '' && !supportedProtocols.includes(url.protocol || '');
27
+ const noPath = url.pathname === '';
28
+ const noSearch = url.search === '';
29
+ const noHash = url.hash === '';
30
+ const hashLink = noPath && noSearch;
31
+ const location = {
32
+ pathname: (noPath
33
+ ? current.pathname
34
+ : url?.pathname),
35
+ search: (hashLink
36
+ ? current.search
37
+ : url?.search),
38
+ hash: (noHash
39
+ ? '#'
40
+ : url?.hash)
41
+ };
42
+ const basicAnchor = (forceExternal ||
43
+ unsupported ||
44
+ url.hostname ||
45
+ !isMounted && hashLink ||
46
+ hashLink && noHash);
47
+ return (basicAnchor
48
+ ? ((0, jsx_runtime_1.jsx)("a", { ...attrs, className: classes(), href: href, children: children }))
49
+ : ((0, jsx_runtime_1.jsx)(router_1.Link, { ...attrs, "aria-current": active ? 'page' : undefined, className: classes(), to: location, children: children })));
50
+ };
51
+ const Anchor = ({ children, classBlock, classModifiers, className, forceExternal, href, ...attrs }) => {
52
+ const classes = (0, component_helpers_1.classBuilder)('penultimate-anchor', classBlock, classModifiers, className);
53
+ const props = {
54
+ ...attrs,
55
+ classBlock,
56
+ classModifiers,
57
+ className,
58
+ forceExternal,
59
+ href
60
+ };
61
+ const content = ((0, jsx_runtime_1.jsx)(AnchorInner, { ...props, children: children }));
62
+ return (!router_1.needSuspense ? content : ((0, jsx_runtime_1.jsx)(react_1.Suspense, { fallback: (0, jsx_runtime_1.jsx)("a", { ...attrs, className: classes(), href: href, children: children }), children: content })));
63
+ };
64
+ exports.Anchor = Anchor;
65
+ exports.Anchor.displayName = 'A';
66
+ exports.default = exports.Anchor;
67
+ exports.A = exports.Anchor;
@@ -0,0 +1,63 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { Suspense } from 'react';
4
+ import { useIsMounted } from '@react-foundry/client-component-helpers';
5
+ import { classBuilder } from '@react-foundry/component-helpers';
6
+ import { Link, needSuspense, useLocation, useIsActive } from '@react-foundry/router';
7
+ import { URI } from '@react-foundry/uri';
8
+ import '../assets/Anchor.scss';
9
+ const supportedProtocols = [
10
+ 'http:',
11
+ 'https:'
12
+ ];
13
+ const AnchorInner = ({ children, classBlock, classModifiers: _classModifiers = [], className, forceExternal = false, href, ...attrs }) => {
14
+ const isMounted = useIsMounted();
15
+ const active = useIsActive()(href || '');
16
+ const current = useLocation();
17
+ const classModifiers = [
18
+ active ? 'active' : '',
19
+ ...(Array.isArray(_classModifiers) ? _classModifiers : [_classModifiers])
20
+ ];
21
+ const classes = classBuilder('penultimate-anchor', classBlock, classModifiers, className);
22
+ const url = URI.parse(href || '');
23
+ const unsupported = url.protocol !== '' && !supportedProtocols.includes(url.protocol || '');
24
+ const noPath = url.pathname === '';
25
+ const noSearch = url.search === '';
26
+ const noHash = url.hash === '';
27
+ const hashLink = noPath && noSearch;
28
+ const location = {
29
+ pathname: (noPath
30
+ ? current.pathname
31
+ : url?.pathname),
32
+ search: (hashLink
33
+ ? current.search
34
+ : url?.search),
35
+ hash: (noHash
36
+ ? '#'
37
+ : url?.hash)
38
+ };
39
+ const basicAnchor = (forceExternal ||
40
+ unsupported ||
41
+ url.hostname ||
42
+ !isMounted && hashLink ||
43
+ hashLink && noHash);
44
+ return (basicAnchor
45
+ ? (_jsx("a", { ...attrs, className: classes(), href: href, children: children }))
46
+ : (_jsx(Link, { ...attrs, "aria-current": active ? 'page' : undefined, className: classes(), to: location, children: children })));
47
+ };
48
+ export const Anchor = ({ children, classBlock, classModifiers, className, forceExternal, href, ...attrs }) => {
49
+ const classes = classBuilder('penultimate-anchor', classBlock, classModifiers, className);
50
+ const props = {
51
+ ...attrs,
52
+ classBlock,
53
+ classModifiers,
54
+ className,
55
+ forceExternal,
56
+ href
57
+ };
58
+ const content = (_jsx(AnchorInner, { ...props, children: children }));
59
+ return (!needSuspense ? content : (_jsx(Suspense, { fallback: _jsx("a", { ...attrs, className: classes(), href: href, children: children }), children: content })));
60
+ };
61
+ Anchor.displayName = 'A';
62
+ export default Anchor;
63
+ export const A = Anchor;
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@react-foundry/anchor",
3
+ "version": "0.1.0",
4
+ "description": "A drop-in replacement for the 'a' element.",
5
+ "main": "dist/Anchor.js",
6
+ "sass": "assets/Anchor.scss",
7
+ "exports": {
8
+ ".": {
9
+ "sass": "./assets/Anchor.scss",
10
+ "types": "./dist/Anchor.d.ts",
11
+ "import": "./dist/Anchor.mjs",
12
+ "require": "./dist/Anchor.js",
13
+ "default": "./dist/Anchor.mjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "/assets",
18
+ "/dist"
19
+ ],
20
+ "author": "Daniel A.C. Martin <npm@daniel-martin.co.uk> (http://daniel-martin.co.uk/)",
21
+ "license": "MIT",
22
+ "keywords": [
23
+ "react-components"
24
+ ],
25
+ "dependencies": {
26
+ "@react-foundry/client-component-helpers": "^0.1.0",
27
+ "@react-foundry/component-helpers": "^0.1.0",
28
+ "@react-foundry/router": "^0.1.0",
29
+ "@react-foundry/uri": "^0.1.0"
30
+ },
31
+ "peerDependencies": {
32
+ "@react-foundry/docs-components": "^0.1.0",
33
+ "@storybook/addon-docs": "^9.1.17",
34
+ "react": "^19.2.3"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "@react-foundry/docs-components": {
38
+ "optional": true
39
+ },
40
+ "@storybook/addon-docs": {
41
+ "optional": true
42
+ }
43
+ },
44
+ "devDependencies": {
45
+ "@types/react": "19.2.9",
46
+ "jest": "30.2.0",
47
+ "jest-environment-jsdom": "30.2.0",
48
+ "ts-jest": "29.4.6",
49
+ "typescript": "5.9.3",
50
+ "@react-foundry/component-test-helpers": "0.1.0"
51
+ },
52
+ "scripts": {
53
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest",
54
+ "build": "npm run build:esm && npm run build:cjs",
55
+ "build:esm": "tsc -m es2022 && find dist -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\;",
56
+ "build:cjs": "tsc",
57
+ "clean": "rm -rf dist tsconfig.tsbuildinfo"
58
+ },
59
+ "module": "dist/Anchor.mjs",
60
+ "typings": "dist/Anchor.d.ts"
61
+ }