@govtechsg/sgds-web-component 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3231,6 +3231,7 @@
3231
3231
 
3232
3232
  var css_248z$1 = css`:host{--accordion-active-color:var(--sgds-primary)}`;
3233
3233
 
3234
+ const VALID_KEYS = ["Enter", "ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight"];
3234
3235
  /**
3235
3236
  * @summary A dropdown mechanism that allow users to either show or hide related content. `SgdsAccordion` is a wrapper to manage the behaviour for multiple `SgdsAccordionItems`
3236
3237
  * @slot default - slot for accordion-item
@@ -3265,10 +3266,8 @@
3265
3266
  }
3266
3267
  });
3267
3268
  }
3268
- async onToggle(event) {
3269
- // Let the event pass through the DOM so that it can be
3270
- // prevented from the outside if a user so desires.
3271
- if (this.allowMultiple || event.defaultPrevented) {
3269
+ async _onToggle(event) {
3270
+ if (this.allowMultiple) {
3272
3271
  // No toggling when `allowMultiple` or the user prevents it.
3273
3272
  return;
3274
3273
  }
@@ -3285,6 +3284,11 @@
3285
3284
  }
3286
3285
  });
3287
3286
  }
3287
+ async _onKeyboardToggle(event) {
3288
+ if (!VALID_KEYS.includes(event.key))
3289
+ return;
3290
+ return this._onToggle(event);
3291
+ }
3288
3292
  render() {
3289
3293
  return html `
3290
3294
  <div
@@ -3293,7 +3297,7 @@
3293
3297
  [`${this.accordionClasses}`]: this.accordionClasses
3294
3298
  })}
3295
3299
  >
3296
- <slot @click=${this.onToggle}></slot>
3300
+ <slot @click=${this._onToggle} @keydown=${this._onKeyboardToggle}></slot>
3297
3301
  </div>
3298
3302
  `;
3299
3303
  }
@@ -3438,7 +3442,7 @@
3438
3442
  };
3439
3443
  }
3440
3444
 
3441
- var css_248z = css`:host{--accordion-item-padding-y:1rem;--accordion-item-padding-x:1.5rem;--accordion-item-border-radius:0.25rem;--accordion-item-font-weight:700;--accordion-item-line-height:2rem}.accordion-body{line-height:var(--accordion-item-line-height);overflow:hidden;padding:0}.accordion-content{display:block;padding:0 var(--accordion-item-padding-x) var(--accordion-item-padding-y)}.accordion-button{line-height:var(--accordion-item-line-height)}.accordion-button:not(.collapsed){box-shadow:none;color:var(--accordion-active-color);font-weight:var(--accordion-item-font-weight)}:host([first-of-type]) .accordion-item{border-radius:var(--accordion-item-border-radius) var(--accordion-item-border-radius) 0 0}:host([nth-of-type]) .accordion-item{border-radius:0;border-top:0}:host([last-of-type]) .accordion-item{border-radius:0 0 var(--accordion-item-border-radius) var(--accordion-item-border-radius);border-top:0}`;
3445
+ var css_248z = css`:host{--accordion-item-padding-y:1rem;--accordion-item-padding-x:1.5rem;--accordion-item-border-radius:0.25rem;--accordion-item-font-weight:700;--accordion-item-line-height:2rem}.accordion-body{line-height:var(--accordion-item-line-height);overflow:hidden;padding:0}.accordion-content{display:block;padding:0 var(--accordion-item-padding-x) var(--accordion-item-padding-y)}.accordion-button{line-height:var(--accordion-item-line-height)}.accordion-button:not(.collapsed){color:var(--accordion-active-color,var(--sgds-primary))}.accordion-button svg.bi-chevron-down{height:1.3rem;margin-left:auto;transition:transform .2s ease-in-out;width:1.3rem}.accordion-button:not(.collapsed){box-shadow:none;font-weight:var(--accordion-item-font-weight)}.accordion-button:not(.collapsed) svg.bi-chevron-down{transform:rotate(-180deg)}.accordion-button:after{content:unset}:host([first-of-type]) .accordion-item{border-radius:var(--accordion-item-border-radius) var(--accordion-item-border-radius) 0 0}:host([nth-of-type]) .accordion-item{border-radius:0;border-top:0}:host([last-of-type]) .accordion-item{border-radius:0 0 var(--accordion-item-border-radius) var(--accordion-item-border-radius);border-top:0}.hidden{display:none}`;
3442
3446
 
3443
3447
  /**
3444
3448
  *
@@ -3467,8 +3471,8 @@
3467
3471
  this.open = false;
3468
3472
  }
3469
3473
  firstUpdated() {
3470
- this.body.hidden = !this.open;
3471
- this.body.style.height = this.open ? "auto" : "0";
3474
+ if (!this.open)
3475
+ this.body.classList.add("hidden");
3472
3476
  }
3473
3477
  handleSummaryClick() {
3474
3478
  if (this.open) {
@@ -3507,10 +3511,9 @@
3507
3511
  return;
3508
3512
  }
3509
3513
  await stopAnimations(this.body);
3510
- this.body.hidden = false;
3514
+ this.body.classList.remove("hidden");
3511
3515
  const { keyframes, options } = getAnimation(this, "accordion.show");
3512
3516
  await animateTo(this.body, shimKeyframesHeightAuto(keyframes, this.body.scrollHeight), options);
3513
- this.body.style.height = "auto";
3514
3517
  this.emit("sgds-after-show");
3515
3518
  }
3516
3519
  else {
@@ -3522,9 +3525,13 @@
3522
3525
  }
3523
3526
  await stopAnimations(this.body);
3524
3527
  const { keyframes, options } = getAnimation(this, "accordion.hide");
3528
+ const animationDuration = options.duration;
3529
+ // Workaround to fix GSIB delay after animateTo.
3530
+ //Setting a timeout of duration slightly less than animation's duraton to prevent case where animation runs faster than .hidden class is added
3531
+ setTimeout(() => {
3532
+ this.body.classList.add("hidden");
3533
+ }, animationDuration - 20);
3525
3534
  await animateTo(this.body, shimKeyframesHeightAuto(keyframes, this.body.scrollHeight), options);
3526
- this.body.hidden = true;
3527
- this.body.style.height = "auto";
3528
3535
  this.emit("sgds-after-hide");
3529
3536
  }
3530
3537
  }
@@ -3550,7 +3557,8 @@
3550
3557
  part="base"
3551
3558
  class=${classMap({
3552
3559
  "sgds accordion-item": true,
3553
- [`${this.accordionItemClasses}`]: this.accordionItemClasses
3560
+ [`${this.accordionItemClasses}`]: this.accordionItemClasses,
3561
+ show: this.open
3554
3562
  })}
3555
3563
  >
3556
3564
  <button
@@ -3567,8 +3575,25 @@
3567
3575
  @keydown=${this.handleSummaryKeyDown}
3568
3576
  >
3569
3577
  <slot name="accordion-header"></slot>
3578
+ <svg
3579
+ xmlns="http://www.w3.org/2000/svg"
3580
+ width="16"
3581
+ height="16"
3582
+ fill="currentColor"
3583
+ class="bi bi-chevron-down"
3584
+ viewBox="0 0 16 16"
3585
+ >
3586
+ <path
3587
+ fill-rule="evenodd"
3588
+ d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z"
3589
+ />
3590
+ </svg>
3570
3591
  </button>
3571
- <div class="accordion-body">
3592
+ <div
3593
+ class=${classMap({
3594
+ "accordion-body": true
3595
+ })}
3596
+ >
3572
3597
  <slot name="accordion-content" class="accordion-content" role="region" aria-labelledby="header"></slot>
3573
3598
  </div>
3574
3599
  </div>
@@ -3599,14 +3624,14 @@
3599
3624
  { height: "0", opacity: "0" },
3600
3625
  { height: "auto", opacity: "1" }
3601
3626
  ],
3602
- options: { duration: 200, easing: "ease-in-out" }
3627
+ options: { duration: 350, easing: "ease-in-out" }
3603
3628
  });
3604
3629
  setDefaultAnimation("accordion.hide", {
3605
3630
  keyframes: [
3606
3631
  { height: "auto", opacity: "1" },
3607
3632
  { height: "0", opacity: "0" }
3608
3633
  ],
3609
- options: { duration: 200, easing: "ease-in-out" }
3634
+ options: { duration: 350, easing: "ease-in-out" }
3610
3635
  });
3611
3636
 
3612
3637
  customElements.define("sgds-accordion", SgdsAccordion);