Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions client/anchorableNode.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import router from './router';
import router from './router'

export function anchorableElement(element) {
const links = element.querySelectorAll('a[href^="/"]:not([target])');
for(const link of links) {
const links = element.querySelectorAll('a[href^="/"]:not([target])')
for (const link of links) {
link.onclick = (event) => {
event.preventDefault();
router.url = link.getAttribute('href');
};
if (event.ctrlKey || event.shiftKey) return
event.preventDefault()
router.url = link.getAttribute('href')
}
}
}
22 changes: 12 additions & 10 deletions plugins/anchorable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ function match(node) {
)
}

function transform({node, router}) {
if(!match(node)) return;
const originalEvent = node.attributes.onclick;
node.attributes.onclick = ({event}) => {
event.preventDefault();
router.url = node.attributes.href;
if(originalEvent) {
function transform({ node, router }) {
if (!match(node)) return
const originalEvent = node.attributes.onclick
node.attributes.default = true
node.attributes.onclick = ({ event }) => {
if (event.ctrlKey || event.shiftKey) return
event.preventDefault()
router.url = node.attributes.href
if (originalEvent) {
setTimeout(() => {
originalEvent({...node.attributes, event});
}, 0);
originalEvent({ ...node.attributes, event })
}, 0)
}
};
}
}

export default { transform, client: true }
20 changes: 20 additions & 0 deletions tests/src/AnchorModifiers.njs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Nullstack from 'nullstack';

class AnchorModifiers extends Nullstack {

html = `
<a href="/anchor-modifiers?source=html">html</a>
`

render() {
return (
<div>
<div html={this.html} />
<a href="/anchor-modifiers?source=jsx">jsx</a>
</div>
)
}

}

export default AnchorModifiers;
39 changes: 39 additions & 0 deletions tests/src/AnchorModifiers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
beforeAll(async () => {
await page.goto('http://localhost:6969/anchor-modifiers');
});

describe('AnchorModifiers jsx', () => {

test('Clicking html link with shift opens in new window', async () => {
await page.keyboard.down('Shift');
await page.click('[href="/anchor-modifiers?source=html"]');
await page.keyboard.up('Shift');
const url = await page.url()
expect(url).toEqual('http://localhost:6969/anchor-modifiers');
});

test('Clicking html link with control opens in new tab', async () => {
await page.keyboard.down('Control');
await page.click('[href="/anchor-modifiers?source=html"]');
await page.keyboard.up('Control');
const url = await page.url()
expect(url).toEqual('http://localhost:6969/anchor-modifiers');
});

test('Clicking jsx link with shift opens in new window', async () => {
await page.keyboard.down('Shift');
await page.click('[href="/anchor-modifiers?source=jsx"]');
await page.keyboard.up('Shift');
const url = await page.url()
expect(url).toEqual('http://localhost:6969/anchor-modifiers');
});

test('Clicking jsx link with control opens in new tab', async () => {
await page.keyboard.down('Control');
await page.click('[href="/anchor-modifiers?source=jsx"]');
await page.keyboard.up('Control');
const url = await page.url()
expect(url).toEqual('http://localhost:6969/anchor-modifiers');
});

});
2 changes: 2 additions & 0 deletions tests/src/Application.njs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Nullstack from 'nullstack';
import AnchorModifiers from './AnchorModifiers';
import './Application.css';
import ChildComponent from './ChildComponent';
import ComponentTernary from './ComponentTernary';
Expand Down Expand Up @@ -107,6 +108,7 @@ class Application extends Nullstack {
<UndefinedNodes route="/undefined-nodes" />
<Purge route="/purge" />
<ComponentTernary route="/component-ternary" />
<AnchorModifiers route="/anchor-modifiers" />
<ErrorPage route="*" />
</main>
)
Expand Down