@rc-component/trigger
Version:
base abstract trigger component for react
23 lines • 807 B
JavaScript
import * as React from 'react';
function toArray(val) {
return val ? Array.isArray(val) ? val : [val] : [];
}
export default function useAction(mobile, action, showAction, hideAction) {
return React.useMemo(() => {
const mergedShowAction = toArray(showAction ?? action);
const mergedHideAction = toArray(hideAction ?? action);
const showActionSet = new Set(mergedShowAction);
const hideActionSet = new Set(mergedHideAction);
if (mobile) {
if (showActionSet.has('hover')) {
showActionSet.delete('hover');
showActionSet.add('click');
}
if (hideActionSet.has('hover')) {
hideActionSet.delete('hover');
hideActionSet.add('click');
}
}
return [showActionSet, hideActionSet];
}, [mobile, action, showAction, hideAction]);
}