Skip to content

Instantly share code, notes, and snippets.

View Sidnioulz's full-sized avatar
🐿️

Steve Dodier-Lazaro Sidnioulz

🐿️
View GitHub Profile
@Sidnioulz
Sidnioulz / FormButton.jsx
Created December 14, 2021 16:46
LJN Reclame Architecture - Gist 14
import PropTypes from '@ljn/prop-types'
import React, { useCallback, useContext } from 'react'
import { FormContext } from 'components/form/Form'
import { useReclame } from 'core/reclame'
import ButtonBase, { renderRootProps } from './partials'
export const FormButton = (initialProps) => {
const params = useReclame(FormButton, initialProps, [
@Sidnioulz
Sidnioulz / avatar-partials.js
Created December 14, 2021 16:43
LJN Reclame Architecture - Gist 13
const AvatarBase = feature({
id: 'AvatarBase',
className: 'avatar',
mixes: [Aria, Clickable, Identifiable, LoadableImage, Sizeable],
propTypes: {
url: PropTypes.string,
name: PropTypes.string,
shorthand: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
},
propDescriptions: {
@Sidnioulz
Sidnioulz / core-feature.js
Last active January 21, 2022 17:40
LJN Reclame Architecture - Gist 15
/**
* Takes a bunch of features, and merges the features' prop types, descriptions
* and default values into an object. If you pass a second parameter, it must be
* the object into which to mix the properties. Else, a blank object will be
* created and returned.
* @param {Array.<object>} features The array of features to mix.
* @param {Function|object=} dest If set, the object to mix into.
* @returns {object} The mixed object (`dest` if it was set).
*/
const mix = (features, dest = { id: Symbol('mix') }) => {
@Sidnioulz
Sidnioulz / Callout.jsx
Last active December 14, 2021 16:19
LJN Reclame Architecture - Gist 12
export const Callout = (initialProps) => {
const params = useReclame(Callout, initialProps, [CalloutBase, Closable])
const { bem, props, features, state } = params
return features.closable.isClosed ? null : (
<aside className={bem.block()} {...renderRootProps(params)}>
{/* ... */}
</aside>
)
}
@Sidnioulz
Sidnioulz / Closable.js
Last active December 15, 2021 12:59
LJN Reclame Architecture - Gist 11
import { useCallback, useState } from 'react'
import PropTypes from '@ljn/prop-types'
import feature from 'core/feature'
/**
* Makes it possible to close a component, by interacting with a clickable element
* that must then trigger `features.closable.close`.
*
* @param {object} params The Reclame parameters of the component using this feature.
@Sidnioulz
Sidnioulz / Focusable.js
Last active December 14, 2021 16:04
LJN Reclame Architecture - Gist 10
import PropTypes from '@ljn/prop-types'
import Disableable from 'interfaces/Disableable'
import feature from 'core/feature'
/**
* Allows focusing a target element.
* @param {object} params The Reclame parameters of the instance during init.
* @returns {object} The following feature state:
* <ul>
@Sidnioulz
Sidnioulz / VariantAble.js
Last active December 14, 2021 15:48
LJN Reclame Architecture - Gist 9
import { isString } from '@ljn/utils'
import PropTypes from '@ljn/prop-types'
import feature from 'core/feature'
const VariantAble = feature({
id: 'variantAble',
modifiers: ({ props }) => {
return { variant: isString(props.variant) ? props.variant.split(' ') : props.variant }
},
@Sidnioulz
Sidnioulz / Translatable.jsx
Created December 14, 2021 13:57
LJN Reclame Architecture - Gist 8
import { useTranslation } from 'react-i18next'
import feature from 'core/feature'
import i18nInstance from 'core/i18n'
/**
* Provides a translation function and i18n instance to Reclame components using
* the Translatable feature. The function is tied to the Reclame scope and Reclame's
* current locale.
* @param {object} params The parameters of the Reclame component.
@Sidnioulz
Sidnioulz / button-features-fileinfos.jsx
Last active January 21, 2022 17:39
LJN Reclame Architecture - Gist 7
const renderFileInfos = (params) => {
const { bem, features, props } = params
return !isNil(props.name) || !isNil(props.type) || !isNil(props.size) ? (
<span className={bem.element('metadata')}>
{props.format?.(params) || features.fileinfos.defaultFormat(params)}
</span>
) : null
}
@Sidnioulz
Sidnioulz / Highlight.jsx
Last active January 28, 2022 14:13
LJN Reclame Architecture - Gist 6
export const SomeSpecialHighlight = (initialProps) => {
// Instance initialisation; we mix the core feature and variant specific feature(s)
const params = useReclame(Highlight, initialProps, [HighlightBase, SomeSpecialFeature])
const { bem, props } = params
// We try to avoid code here; we prefer to do state management and compute
// memoised values inside features' init hooks when possible.This keeps
// final component code as close to being a DOM declaration as possible
// and allows us to make variants with a rearranged DOM at next to no cost.