I'd like to display a "header" for those instances where my Lightning component is not displayed in a tab, but directly on the page. Is there a way to determine if a Lightning component is being displayed in a tab?
-
What is the difference here between a tab and a page? Is your tab a custom tab that you have created and is the page a record page?– Jayant DasMay 14, 2019 at 13:51
-
@JayantDas the tab would be the Standard Lightning component you add to a Lightning page. Its called "Tabs".– Michael SobczakMay 14, 2019 at 14:13
-
@JayantDas I added a picture to show what I'm referring to.– Michael SobczakMay 14, 2019 at 14:18
2 Answers
Let me offer you another approach.
Instead of trying to figure out where a component is dropped, think about exposing a design attribute on your component that will allow users to show or hide the header manually when they manipulate it in the App Builder.
demoComponent.cmp
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="showHeader" type="boolean" />
<aura:if isTrue="{!v.showHeader}">
<h1>This is a header</h1>
</aura:if>
<div>Something else</div>
</aura:component>
demoComponent.design
<design:component>
<design:attribute name="showHeader" label="Show Header?" default="false"/>
</design:component>
The above code will result in Show Header?
property being available to a user in the Lightning App Builder:
-
1I like this approach. I hadn't used design attributes much, but this makes a lot of sense in this context. Thanks! Jun 5, 2019 at 20:29
So in layman's term, you want to determine what is the parent of your child component.
No, This is not possible and this should never be possible as this contains a great security risk. Imagine any rogue app exchange application reading the parent's component data(in your case tabs) and doing the malicious operation.
That being said, why should a component behave differently based on where its dropped? The component should expose a few properties which parent can fill in and change behavior if needed.
-
I'm not sure what you are saying makes sense. Using the workspaceAPI from within an Aura Lightning component, I can get tabs in the current console. Would that not be a security risk, as you put it? Jun 5, 2019 at 20:11
-
Workspace API allows you to open, close tabs but does not allow you read content. On little subset of funtions which wont posses security risk are allowed Jun 5, 2019 at 20:21