@maskedeng-tom/ssrsx
Version:
server side renderer with tsx
101 lines • 4.67 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavLink = exports.Link = void 0;
const jsx_runtime_1 = require("ssrsxjsx/jsx-runtime");
const useLocation_1 = require("./hooks/useLocation");
const useHref_1 = require("./hooks/useHref");
////////////////////////////////////////////////////////////////////////////////
const AriaCurrent = 'aria-current';
////////////////////////////////////////////////////////////////////////////////
const Link = (_a) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
var { tag, to, end, caseSensitive, event, children } = _a, rest = __rest(_a, ["tag", "to", "end", "caseSensitive", "event", "children"]);
const location = (0, useLocation_1.useLocation)();
// parse href
let search = '';
let targetUrl = to;
const searchPos = to.indexOf('?');
if (searchPos >= 0) {
search = to.slice(searchPos);
targetUrl = to.slice(0, searchPos);
}
// href to full url
const targetFullUrl = (0, useHref_1.useHref)(targetUrl);
// check active
const comparePathname = caseSensitive ? location.pathname : location.pathname.toLowerCase();
const compareTargetUrl = caseSensitive ? targetFullUrl : targetFullUrl.toLowerCase();
let isActive = false;
if (end) {
isActive = comparePathname === compareTargetUrl;
}
else {
isActive = comparePathname.startsWith(compareTargetUrl);
}
// parse props
const { className, style } = rest;
// class name
let outputClassName = undefined;
if (!className || typeof className === 'string') {
outputClassName = (className ? String(className).trim() : '') + (isActive ? ' active' : '');
}
else if (typeof className === 'function') {
outputClassName = String(className(isActive));
}
outputClassName = outputClassName ? outputClassName.trim() : undefined;
// style
let outputStyle = style;
if (typeof style === 'function') {
outputStyle = style(isActive);
}
// children
let outputChildren = children;
if (typeof children === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
outputChildren = children(isActive);
if (outputChildren instanceof Promise) {
outputChildren = yield outputChildren;
}
}
// aria-current
let ariaCurrent = undefined;
if (isActive) {
ariaCurrent = (_b = ((rest[AriaCurrent] !== undefined) ? String(rest[AriaCurrent]) : undefined)) !== null && _b !== void 0 ? _b : 'page';
}
delete rest[AriaCurrent];
// output href
const targetHref = targetFullUrl + search;
// output tag
const outputTag = tag || 'a';
// output event name
const outputEventName = `on${event !== null && event !== void 0 ? event : 'click'}`;
// output tags
return (0, jsx_runtime_1.jsx)(outputTag, Object.assign(Object.assign({ type: (outputTag === 'button') ? 'button' : undefined, href: (outputTag === 'a') ? targetHref : undefined, [outputEventName]: (outputTag === 'a') ?
undefined
:
`js:location.href='${location.realPath(targetHref)}'`, children: outputChildren }, rest), { className: outputClassName, style: outputStyle, [AriaCurrent]: ariaCurrent }));
});
exports.Link = Link;
exports.NavLink = Link;
////////////////////////////////////////////////////////////////////////////////
exports.default = Link;
//# sourceMappingURL=Link.js.map