@pmwcs/ripple
Version:
PMWCS ripple component
220 lines (168 loc) • 11.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withRipple = exports.RippleSurface = exports.Ripple = void 0;
var _preact = require("preact");
var _compat = require("preact/compat");
var _base = require("@pmwcs/base");
var _provider = require("@pmwcs/provider");
var _foundation = require("./foundation");
var _excluded = ["children", "className", "primary", "secondary", "accent", "unbounded", "surface", "domNode", "foundationRef"],
_excluded2 = ["className"],
_excluded3 = ["ripple"];
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var RippleSurfaceContext = (0, _preact.createContext)({});
/** A component for adding Ripples to other components. */
var withDomNode = function withDomNode() {
return function (WrapComponent) {
return /*#__PURE__*/function (_Component) {
_inherits(_class, _Component);
var _super = _createSuper(_class);
function _class() {
var _this;
_classCallCheck(this, _class);
_this = _super.call(this);
_this.state = {
domNode: null
};
return _this;
}
_createClass(_class, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
domNode: (0, _compat.findDOMNode)(this)
});
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
var rootRippleElement = (0, _compat.findDOMNode)(this);
if (rootRippleElement !== this.state.domNode) {
this.setState({
rootRippleElement: rootRippleElement
});
}
}
}, {
key: "render",
value: function render() {
return (0, _preact.h)(WrapComponent, _extends({}, this.props, {
domNode: this.state.domNode
}));
}
}]);
return _class;
}(_preact.Component);
};
};
var Ripple = withDomNode()(function Ripple(props) {
var children = props.children,
className = props.className,
primary = props.primary,
secondary = props.secondary,
accent = props.accent,
unbounded = props.unbounded,
surface = props.surface,
domNode = props.domNode,
foundationRef = props.foundationRef,
rest = _objectWithoutProperties(props, _excluded);
var _useRippleFoundation = (0, _foundation.useRippleFoundation)(props),
rootEl = _useRippleFoundation.rootEl,
surfaceEl = _useRippleFoundation.surfaceEl;
var child = _compat.Children.only(children);
if (!(0, _preact.isValidElement)(child)) {
return null;
} // This flag really determines a lot
// is surfaceIsRoot is true, then the surface props are spread
// to the underlying component, otherwise the only place they
// can be picked up is by the context consumer
var surfaceIsRoot = !surface || !unbounded;
var unboundedProp = unbounded ? {
'data-mdc-ripple-is-unbounded': true
} : {};
var rippleSurfaceProps = surfaceIsRoot ? surfaceEl.props({
style: child.props.style
}) : {};
var finalClassNames = (0, _base.classNames)(className !== child.props.className && className, rippleSurfaceProps.className, child.props.className, {
'mdc-ripple-surface': typeof surface === 'boolean' ? surface : surface === undefined,
'mdc-ripple-surface--primary': primary,
'mdc-ripple-surface--accent': accent || secondary
}); // Fixes a ripple artifact issue
// that is caused when clicking a button disables it
// https://codesandbox.io/s/842vo56019
if (rest.disabled) {
finalClassNames = finalClassNames.replace('mdc-ripple-upgraded--background-focused', '');
} // do some crazy props merging...
var content = (0, _preact.cloneElement)(child, _objectSpread(_objectSpread(_objectSpread({}, child.props), unboundedProp), rootEl.props(_objectSpread(_objectSpread(_objectSpread({}, rest), {}, {
style: child.props.style
}, rippleSurfaceProps), {}, {
className: finalClassNames
}))));
return (0, _preact.h)(RippleSurfaceContext.Provider, {
value: surfaceEl.props({
style: child.props.style
})
}, content);
});
exports.Ripple = Ripple;
var RippleSurface = function RippleSurface(_ref) {
var className = _ref.className,
rest = _objectWithoutProperties(_ref, _excluded2);
return (0, _preact.h)(RippleSurfaceContext.Consumer, null, function (rippleSurfaceProps) {
return (0, _preact.h)("div", _extends({}, rest, rippleSurfaceProps, {
className: "".concat(className, " ").concat(rippleSurfaceProps.className || '')
}));
});
};
/**
* HOC that adds ripples to any component
*/
exports.RippleSurface = RippleSurface;
var withRipple = function withRipple(_ref2) {
var defaultUnbounded = _ref2.unbounded,
defaultAccent = _ref2.accent,
defaultSurface = _ref2.surface;
return function (WrapComponent) {
var WithRippleComponent = (0, _compat.forwardRef)(function (_ref3, ref) {
var _ripple;
var ripple = _ref3.ripple,
rest = _objectWithoutProperties(_ref3, _excluded3);
var providerContext = (0, _provider.useProviderContext)();
ripple = (_ripple = ripple) !== null && _ripple !== void 0 ? _ripple : providerContext.ripple;
var rippleOptions = _typeof(ripple) !== 'object' ? {} : ripple;
if (ripple) {
return (0, _preact.h)(Ripple, _extends({}, rest, {
accent: rippleOptions.accent || defaultAccent,
unbounded: rippleOptions.unbounded || defaultUnbounded,
surface: rippleOptions.surface || defaultSurface
}), (0, _preact.h)(WrapComponent, _extends({}, rest, {
ref: ref
})));
}
return (0, _preact.h)(WrapComponent, _extends({}, rest, {
ref: ref
}));
});
WithRippleComponent.displayName = "withRipple(".concat(WrapComponent.displayName || WrapComponent.constructor.name || 'Unknown', ")");
return WithRippleComponent;
};
};
exports.withRipple = withRipple;