@atlaskit/pragmatic-drag-and-drop-react-beautiful-dnd-migration
Version:
An optional Pragmatic drag and drop package that enables rapid migration from react-beautiful-dnd to Pragmatic drag and drop
97 lines (91 loc) • 2.77 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
/**
* This is a vastly simplified version of the style marshal in `react-beautiful-dnd`.
*
* Most of the styles have been removed, as they are not required for native dragging.
* They were only required in `react-beautiful-dnd` because it emulated dragging.
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import { useLayoutEffect } from '../../hooks/use-isomorphic-layout-effect';
import { attributes } from '../../utils/attributes';
/**
* Used to uniquely identify the style element.
*/
var styleContextIdAttribute = 'data-rbd-style-context-id';
/**
* Returns the CSS string for the rule with the given selector and style
* declarations.
*/
function getRuleString(_ref) {
var selector = _ref.selector,
styles = _ref.styles;
var concatString = Object.entries(styles).map(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 2),
property = _ref3[0],
value = _ref3[1];
return "".concat(property, ": ").concat(value, ";");
}).join(' ');
return "".concat(selector, " { ").concat(concatString, " }");
}
/**
* Returns the rule string for drag handle styles.
*/
export function getDragHandleRuleString(contextId) {
var selector = "[".concat(attributes.dragHandle.contextId, "=\"").concat(contextId, "\"]");
var styles = {
/**
* Indicates the element is draggable.
*
* Although this is always applied, it will not be visible during drags
* because the browser will override the cursor.
*/
cursor: 'grab',
/**
* Improves the UX when dragging links on iOS.
*
* Without this a preview of the link will open. Although it is still
* draggable, it is inconsistent with `react-beautiful-dnd`.
*/
'-webkit-touch-callout': 'none'
};
return getRuleString({
selector: selector,
styles: styles
});
}
function createStyleEl(_ref4) {
var contextId = _ref4.contextId,
nonce = _ref4.nonce;
var el = document.createElement('style');
if (nonce) {
el.setAttribute('nonce', nonce);
}
el.setAttribute(styleContextIdAttribute, contextId);
document.head.appendChild(el);
return el;
}
function createStyleManager(_ref5) {
var contextId = _ref5.contextId,
nonce = _ref5.nonce;
var el = createStyleEl({
contextId: contextId,
nonce: nonce
});
/**
* Inject the style content.
*/
el.textContent = getDragHandleRuleString(contextId);
return function cleanup() {
el.remove();
};
}
export default function useStyleMarshal(_ref6) {
var contextId = _ref6.contextId,
nonce = _ref6.nonce;
useLayoutEffect(function () {
return createStyleManager({
contextId: contextId,
nonce: nonce
});
}, [contextId, nonce]);
}