Skip to content

Instantly share code, notes, and snippets.

View iMoses's full-sized avatar

iMoses iMoses

  • ag-Grid
  • London, UK
View GitHub Profile
@iMoses
iMoses / Collapsible.jsx
Last active September 9, 2022 19:20
react-headless-collapsible: a React hook to create collapsible components (inspired by `react-collapsible`)
import { useCollapsible } from './use-collapsible';
export function Collapsible({ open, title, className, lazyLoad, onToggle, children, ...props }) {
const { isOpen, shouldRender, getTriggerProps, getContentProps } = useCollapsible({ open, onToggle });
const classNames = [className, 'collapsible'];
if (isOpen) classNames.push('is-open');
return (
<div {...props} className={classNames.join(' ')}>
<header className="collapsible-trigger" {...getTriggerProps()}>
function stringToHsl(
str,
{ saturation = 50, lightness = 50, range: [minHue, maxHue] = [0, 360] } = {}
) {
const hash = str.split('').reduce((hash, char) => char.charCodeAt(0) + ((hash << 5) - hash), 0);
return `hsl(${(Math.abs(hash) % (maxHue - minHue)) + minHue}, ${saturation}%, ${lightness}%)`;
}
export const qsPrimitivesDecoder = (value, defaultDecoder, charset, type) =>
(type === "key" ? defaultDecoder : convertFromString)(value);
function convertFromString(value) {
if (typeof value === "undefined" || value === "") {
return null;
} else if (value === "false" || value === "true") {
return value === "true";
} else if (Array.isArray(value)) {
return value.map(convertFromString);
import { serialize, deserialize, serializable, custom } from 'serializr';
import { deepObserve } from 'mobx-utils';
import storage from 'utils/storage';
import _ from 'lodash';
export const deserializable = serializable(custom(() => {}, v => v));
export const fromStorage = (model, defaults = {}) =>
deserialize(model, _.merge(defaults, storage.get(model.name, {})));
@iMoses
iMoses / css-modules.js
Created May 15, 2017 20:50
Automatically pass React elements 'className' into the 'classnames' library after binding with css-modules styles
import classNames from 'classnames/bind';
import React from 'react';
export default styles => Component => isStateless(Component)
? props => transformElement(Component(props), classNames.bind(styles))
: class extends Component { render() { return transformElement(super.render(), classNames.bind(styles)); } };
function transformElement(el, cx) {
let className = el && el.props.className;
if (className) {
// a simple implementation of a css modules decorator which utilizes the
// classNames package and automatically overrides the original className
import classNames from 'classnames/bind';
import React from 'react';
export default styles => Component => isStateless(Component)
? props => transformElement(Component(props), classNames.bind(styles))
: class extends Component { render() { return transformElement(super.render(), classNames.bind(styles)); } };
@iMoses
iMoses / reducer.js
Last active November 7, 2016 14:15
Immutable Records as stores (minimal example)
import stateRecord from './state-record';
import {
FETCH_SEARCH_RESULTS_LOADING,
FETCH_SEARCH_RESULTS_SUCCESS,
FETCH_SEARCH_RESULTS_FAILURE,
} from './actions';
export default {
stateRecord,
actionHandlers: {
@iMoses
iMoses / jquery.plugin.wrapInGroups.js
Created July 26, 2012 12:21
jQuery Plugin wrapInGroups
/**
* Takes a jQuery collection and wraps each 'groupSize' items with 'wrapHtml'
* Example: $('#Example span').wrapInGroups('<div/>', 4);
* Will wrap every 4 span's inside #Example with a div
* See for yourself: http://jsfiddle.net/M9ZFh/
* Author: iMoses (imoses.g@gmail.com)
*/
(function($) {
$.fn.wrapInGroups = function(wrapHtml, groupSize) {
var selector = this.selector;