ExamplesPlaygroundReference Source

coral-spectrum/coral-component-panelstack/src/scripts/Panel.js

  1. /**
  2. * Copyright 2019 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. import {BaseComponent} from '../../../coral-base-component';
  14. import {transform} from '../../../coral-utils';
  15. import {Decorator} from '../../../coral-decorator';
  16.  
  17. const CLASSNAME = '_coral-Panel';
  18.  
  19. /**
  20. @class Coral.Panel
  21. @classdesc A Panel component
  22. @htmltag coral-panel
  23. @extends {HTMLElement}
  24. @extends {BaseComponent}
  25. */
  26. const Panel = Decorator(class extends BaseComponent(HTMLElement) {
  27. /** @ignore */
  28. constructor() {
  29. super();
  30.  
  31. // Templates
  32. this._elements = {
  33. content: this.querySelector('coral-panel-content') || document.createElement('coral-panel-content')
  34. };
  35. }
  36.  
  37. /**
  38. The content of the panel.
  39.  
  40. @type {PanelContent}
  41. @contentzone
  42. */
  43. get content() {
  44. return this._getContentZone(this._elements.content);
  45. }
  46.  
  47. set content(value) {
  48. this._setContentZone('content', value, {
  49. handle: 'content',
  50. tagName: 'coral-panel-content',
  51. insert: function (content) {
  52. this.appendChild(content);
  53. }
  54. });
  55. }
  56.  
  57. /**
  58. Whether the item is selected. When true, the item will appear as the active element in the PanelStack. The item
  59. must be a child of a PanelStack before this property is set to true. This property cannot be programmatically set
  60. to false.
  61.  
  62. @type {Boolean}
  63. @default false
  64. @htmlattribute selected
  65. @htmlattributereflected
  66. */
  67. get selected() {
  68. return this._selected || false;
  69. }
  70.  
  71. set selected(value) {
  72. let _selected = transform.booleanAttr(value);
  73.  
  74. if(this._selected === _selected) {
  75. return;
  76. }
  77.  
  78. this._selected = _selected;
  79. this._reflectAttribute('selected', this._selected);
  80.  
  81. this.classList.toggle('is-selected', this._selected);
  82. this.setAttribute('aria-hidden', !this.selected);
  83.  
  84. this.trigger('coral-panel:_selectedchanged');
  85. }
  86.  
  87. get _contentZones() {
  88. return {'coral-panel-content': 'content'};
  89. }
  90.  
  91. /** @ignore */
  92. static get observedAttributes() {
  93. return super.observedAttributes.concat(['selected']);
  94. }
  95.  
  96. /** @ignore */
  97. render() {
  98. super.render();
  99.  
  100. this.classList.add(CLASSNAME);
  101.  
  102. // Adds the role to support accessibility when role is not already defined.
  103. if (!this.hasAttribute('role')) {
  104. this.setAttribute('role', 'region');
  105. }
  106.  
  107. // Fetch the content zone elements
  108. const content = this._elements.content;
  109.  
  110. // Move the content into the content zone if none specified
  111. if (!content.parentNode) {
  112. while (this.firstChild) {
  113. content.appendChild(this.firstChild);
  114. }
  115. }
  116.  
  117. // Assign the content zone so the insert function will be called
  118. this.content = content;
  119. }
  120. });
  121.  
  122. export default Panel;