# All files / behaviors/ScrollShadow ScrollShadow.js

0% Statements 0/112
0% Branches 0/44
0% Functions 0/19
0% Lines 0/51

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277

     import React, { createContext, createRef } from 'react';
    import { createPortal } from 'react-dom';
    import PropTypes from 'prop-types';
    import Sentinel from './Sentinel';
    import ShadowOverlay from './ShadowOverlay';
    import DefaultContainer from './DefaultContainer';
    import DefaultScrollContainer from './DefaultScrollContainer';

    const { Provider, Consumer } = createContext(() => () => {});

    /**
     * A scrolling container which has a shadow effect to indicate that there is more
     * content above or below. This element also creates a context which can inform
     * components below it in the tree of the scroll state of the scrolling container.
     */
    export default class ScrollShadow extends React.Component {
      static propTypes = {
        /**
         * This ScrollShadow should listen to the global window scroll value. Only one
         * such ScrollShadow should be present in your application.
         */
        global: PropTypes.bool,
        /**
         * Customize the outer container element by passing a component here. Your element
         * MUST have relative or absolute positioning applied!
         */
        Container: PropTypes.func,
        /**
         * Customize the scroll container element by passing a component here. Your element
         * MUST have relative or absolute positioning, an overflow mode, and accept an
         * innerRef prop!
         */
        ScrollContainer: PropTypes.func,
        /**
         * Flip this to true to show a red line where the scroll sentinel elements are.
         * Can be useful for debugging if the shadows don't seem to work correctly.
         */
        debugShowSentinels: PropTypes.bool,
      };

      static defaultProps = {
        global: false,
        Container: DefaultContainer,
        ScrollContainer: DefaultScrollContainer,
        debugShowSentinels: false,
      };

      /**
       * A sentinel is an invisible element which is used to track the scroll boundaries.
       * We place them at the top and bottom of the content. When either of them becomes
       * visible, we know that the user has hit that particular scroll boundary.
       */
      topSentinelRef = createRef();
      bottomSentinelRef = createRef();
      /**
       * We are not storing this information in this.state on purpose. We will actually
       * be skipping the React update cycle entirely for changing the shadow styles
       * on the shadow element to avoid unnecessary work for a simple aesthetic change.
       */
      isTopSentinelVisible = true;
      isBottomSentinelVisible = false;

      componentDidMount() {
        if (this.props.global) {
          /**
           * In global mode, the entire window is our scroll context. We can do that by
           * passing 'null' as the context element for the IntersectionObserver.
           * As documented, it will use the visible window area as the intersection
           * boundary.
           */
          this.scrollElementRef(null);
        }
      }

      /**
       * Simply figures out where the shadows should be based on which boundaries
       * are visible right now.
       */
      calcShadowMode = () => {
        const top = !this.isTopSentinelVisible;
        const bottom = !this.isBottomSentinelVisible;

        if (top && bottom) {
          return 'both';
        } else if (top) {
          return 'top';
        } else if (bottom) {
          return 'bottom';
        } else {
          return 'none';
        }
      };

      /**
       * This will be called every time the intersection state of either sentinel changes.
       */
      handleSentinelIntersection = (entries, observer) => {
        // `entries` is an array of records, one per changed sentinel.
        entries.forEach(entry => {
          if (entry.target === this.topSentinelRef.current) {
            this.isTopSentinelVisible = entry.isIntersecting;
          } else if (entry.target === this.bottomSentinelRef.current) {
            this.isBottomSentinelVisible = entry.isIntersecting;
          }

          const currentMode = this.calcShadowMode();

          /**
           * Iterate over every registered shadow element to update their styles
           * if they need to change.
           */
          Object.values(this.shadowElementRegistrations).forEach(registration => {
            const { ref, mode } = registration;
            // we don't want to recalculate styles needlessly, so we also ensure
            // the mode has changed since last style assignment
            if (ref.current && mode !== currentMode) {
              // reset classes
              ref.current.classList.remove('top', 'bottom', 'both');
              ref.current.classList.add(currentMode);
              registration.mode = currentMode;
            }
          });
        });
      };

      scrollElementRef = el => {
        if (this.sentinelIntersectionObserver) {
          this.sentinelIntersectionObserver.disconnect();
        }

        this.sentinelIntersectionObserver = new window.IntersectionObserver(
          this.handleSentinelIntersection,
          {
            root: el,
          },
        );
        this.sentinelIntersectionObserver.observe(this.topSentinelRef.current);
        this.sentinelIntersectionObserver.observe(this.bottomSentinelRef.current);
      };

      /**
       * A map of elements which have registered that they want a shadow applied to them.
       * Elements can register by using the `getShadowElementRef` function we pass through
       * the Context.
       */
      shadowElementRegistrations = {};

      getShadowElementRef = name => {
        if (!this.shadowElementRegistrations[name]) {
          this.shadowElementRegistrations[name] = {
            ref: createRef(),
          };
        }

        return this.shadowElementRegistrations[name].ref;
      };

      renderGlobalSentinels = () =>
        createPortal(
          <React.Fragment>
            <Sentinel
              innerRef={this.topSentinelRef}
              className="scroll-shadow-sentinel top global"
              debugShow={this.props.debugShowSentinels}
            />
            <Sentinel
              innerRef={this.bottomSentinelRef}
              className="scroll-shadow-sentinel bottom global"
              debugShow={this.props.debugShowSentinels}
            />
          </React.Fragment>,
          window.document.body,
        );

      renderChildren = () => {
        const {
          children,
          global,
          Container,
          ScrollContainer,
          disabled,
          outer,
          ...extraProps
        } = this.props;

        // we will be rendering something slightly different if we are in global mode
        if (global) {
          return (
            <React.Fragment>
              {children}
              {this.renderGlobalSentinels()}
              {!disabled && (
                <ShadowOverlay
                  className={`scroll-shadow inner`}
                  entireViewport
                  innerRef={this.getShadowElementRef('built-in')}
                />
              )}
            </React.Fragment>
          );
        }

        const shadowPlacement = outer ? 'outer' : 'inner';

        return (
          <Container className="scroll-shadow-container" {...extraProps}>
            <ScrollContainer innerRef={this.scrollElementRef} {...extraProps}>
              <Sentinel
                innerRef={this.topSentinelRef}
                className="scroll-shadow-sentinel top"
                debugShow={this.props.debugShowSentinels}
              />
              {children}
              <Sentinel
                innerRef={this.bottomSentinelRef}
                className="scroll-shadow-sentinel bottom"
                debugShow={this.props.debugShowSentinels}
              />
            </ScrollContainer>
            {!disabled && (
              <ShadowOverlay
                className={`scroll-shadow ${shadowPlacement}`}
                innerRef={this.getShadowElementRef('built-in')}
              />
            )}
          </Container>
        );
      };

      render() {
        return (
          <Provider value={this.getShadowElementRef}>
            {this.renderChildren()}
          </Provider>
        );
      }
    }

    /**
     * A ConnectedShadow is a shadow element which gets its shadow mode from
     * a ScrollShadow context higher in the React tree. Using this, you can
     * add some shadows to 'unrelated' elements which respond to the
     * scroll state of the containing scroll element. This is good for
     * advanced use cases, like rendering an element which hovers over
     * a scroll container.
     */
    export class ConnectedShadow extends React.PureComponent {
      generatedName = `shadow${Math.floor(Math.random() * 100000)}`;

      render() {
        const {
          outer,
          entireViewport,
          disabled,
          name = this.generatedName,
        } = this.props;

        if (disabled) {
          return null;
        }

        return (
          <Consumer>
            {getShadowElementRef => (
              <ShadowOverlay
                className={`scroll-shadow ${outer ? 'outer' : 'inner'}`}
                entireViewport={entireViewport}
                innerRef={getShadowElementRef(name)}
              />
            )}
          </Consumer>
        );
      }
    }

    ScrollShadow.ConnectedShadow = ConnectedShadow;