ExamplesPlaygroundReference Source

coral-spectrum/coral-component-actionbar/src/scripts/getFirstSelectableWrappedItem.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 {commons} from '../../../coral-utils';
  14.  
  15. /** @ignore */
  16. export default function getFirstSelectableWrappedItem(wrapperItem) {
  17. // util method to get first selectable item inside a wrapper item
  18. if (!wrapperItem) {
  19. return null;
  20. }
  21.  
  22. if (wrapperItem.hasAttribute('coral-actionbar-more')) {
  23. // more buttons are no 'real' actionbar items => not wrapped
  24. return wrapperItem;
  25. }
  26.  
  27. let child = null;
  28. for (let i = 0 ; i < wrapperItem.children.length ; i++) {
  29. child = wrapperItem.children[i];
  30.  
  31. // maybe filter even more elements? (opacity, display='none', position='absolute' ...)
  32. if (child.offsetParent && (child.matches(commons.FOCUSABLE_ELEMENT_SELECTOR) || child.matches('a:not([href])')) && !child.hasAttribute('disabled')) {
  33. return child;
  34. }
  35. }
  36.  
  37. // search at 2nd level, some elements like coral-fileupload has selectable items inside them
  38. for (let i = 0 ; i < wrapperItem.children.length ; i++) {
  39. child = wrapperItem.children[i];
  40. for (let j = 0 ; j < child.children.length ; j++) {
  41. let subChild = child.children[j];
  42. // maybe filter even more elements? (opacity, display='none', position='absolute' ...)
  43. if (subChild.offsetParent && (subChild.matches(commons.FOCUSABLE_ELEMENT_SELECTOR) || child.matches('a:not([href])'))) {
  44. return subChild;
  45. }
  46. }
  47. }
  48.  
  49. return null;
  50. }