GithubHelp home page GithubHelp logo

ninoish / lwc-user-friendly-a-tag Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 231 KB

User friendly navigation anchor tag component instead of <a> tag in LWC

License: MIT License

JavaScript 95.04% HTML 4.96%
lightning-web-components lwc

lwc-user-friendly-a-tag's Introduction

LWC User Friendly <a> Tag

Why this needed

NavigationMixin is useful on LWC SPA development, but it doesn't handle browser default behaviours in an easy way, such as..

  • Open in new tab (command + click, ctrl + click)
  • Open in new window (Shift + click)

We also have to set href attribute value in <a> tag to support user right clicking , which value can be fetched by NavigationMixin.GenerateUrl.

This repo packages an example LWC component <c-a-tag> that handles browser default behaviours instead of <a> tag in LWC way. You don't have to use NavigationMixin in your component with this. Just pass a Page Reference object or URL string to href attribute of the component.

Code example

<template>
    <!-- Usecase 1 : Simple anchor -->
    <c-a-tag href={pageRef1}>Link here!</c-a-tag>

    <!-- Usecase 2 : Block style anchor -->
    <c-a-tag href={pageRef2} a-tag-style="display:block;">
        <div>
            <img src="https://example.com/abc123.png"/>
            <p>Hello LWC developers!</p>
        </div>
    </c-a-tag>

    <!-- Usecase 3 : URL String is also available -->
    <c-a-tag href="https://salesforce.com" target="_blank">
        <span>Click here</span>
    </c-a-tag>
<template>
import { LightningElement, track } from "lwc";

export default class ExampleLwc extends LightningElement {

    @track pageRef1;
    @track pageRef2;

    connectedCallback() {
        this.pageRef1 = {
            type: "standard__recordPage",
            attributes: {
                recordId: this.recordId,
                objectApiName: "AnotherCustomObject__c",
                actionName: "view"
            }
        };

        this.pageRef2 = {
            type: 'standard__app',
            attributes: {
                appTarget: 'standard__Sales',
            }
        };
    }

}
  • Available in LEX and Experience Cloud (including LWR ).
  • Supports dynamic pageReference assignment.

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.