1

I have a LWC with a button ("Toggle Sales Coaching) that when selected toggles the slds-is-expanded class off and toggles the slds-is-collapsed class on. I would like for the component to know, for each specific user, what state they left this in and the next time the user loads this component (via renderedCallback()?) it should have that class applied. Is there a good way to do this? Here is my component code:

<template>
<article class="slds-card">
    <div class="slds-card__header slds-grid slds-m-bottom_none slds-p-left_small">
        <div class="slds-path slds-path_has-coaching">
            <button class="slds-float_left slds-button slds-button_icon slds-button_icon-border-filled slds-path__trigger slds-path__trigger_open uibutton" 
                    title="Toggle Sales Coaching" 
                    aria-expanded="false" 
                    aria-controls="path-coaching-2"
                    onclick={handleToggleClick}>
                <svg class="slds-button__icon" aria-hidden="true">
                    <use xlink:href="/_slds/icons/utility-sprite/svg/symbols.svg#chevronright"></use>
                </svg>
                <span class="slds-assistive-text">Show Guidance</span>
            </button>
        </div>
        <div class="slds-p-top_x-small slds-text-title_bold">
            Guidance for Success
        </div>
    </div>

    <div class="slds-card__body slds-card__body_inner slds-m-top_xx-small padLeft">
        <div class="slds-path__coach slds-is-expanded">
            <lightning-formatted-rich-text class="guidanceText" value={label.investmentScoringGuidance}></lightning-formatted-rich-text>
        </div>
    </div>

</article>

and relevant methods in js file:

renderedCallback() {
    //retrieve DocumentCookie here and then apply either then apply the correct class
};

handleToggleClick() {
    const textContainerClassList = this.template.querySelector('.slds-path__coach').classList;
    const buttonClassList = this.template.querySelector('.slds-button').classList;
    //toggle the hidden class on/off
    textContainerClassList.toggle('slds-is-collapsed');
    textContainerClassList.toggle('slds-is-expanded');
    buttonClassList.toggle('slds-path__trigger_open');
};
2
  • This is sort of inferred, but I want to clarify: do you have a specific preference for using cookies to achieve this, vs. storing this type of data server-side in Salesforce? Are you looking to better understand how to store and read cookies in LWC? And have you seen this thread yet: salesforce.stackexchange.com/questions/270325 ...? Mar 12, 2021 at 17:24
  • I figured out how to handle this using document cookies. Thanks Mar 16, 2021 at 21:22

0

You must log in to answer this question.

Browse other questions tagged .