Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Last active June 15, 2022 01:18
Show Gist options
  • Save itsjavi/01b8030b9be7c679eb8962c8ae79b278 to your computer and use it in GitHub Desktop.
Save itsjavi/01b8030b9be7c679eb8962c8ae79b278 to your computer and use it in GitHub Desktop.
WebStorm template for React Components and React CSS Modules. You'll have to enable Live Templates for the END cursor to work.
import React from 'react';
import PropTypes from "prop-types";
import styles from './${NAME}.module.css';
#set($cssClass = ${StringUtils.removeAndHump(${NAME}, ".")})
#set($cssClass = $cssClass.substring(0,1).toLowerCase() + $cssClass.substring(1))
function ${NAME}(props) {
return (
<div className={styles.${cssClass}}>
#[[$END$]]#
{props.children}
</div>
);
}
${NAME}.propTypes = {
required: PropTypes.string.isRequired,
optional: PropTypes.string
}
export default ${NAME}
export {${NAME}}
#set($cssClass = ${StringUtils.removeAndHump(${NAME}, ".")})
#set($cssClass = $cssClass.substring(0,1).toLowerCase() + $cssClass.substring(1))
.${cssClass} {
position:relative;#[[$END$]]#
}
import styles from './${NAME}.module.css';
import React from 'react';
import { classStr, ExtensibleComponentProps } from "@supereffective/shared/core"
#set($cssClass = ${StringUtils.removeAndHump(${NAME}, ".")})
#set($cssClass = $cssClass.substring(0,1).toLowerCase() + $cssClass.substring(1))
export interface ${NAME}Props extends ExtensibleComponentProps {
#[[$END$]]#
}
export const ${NAME} = ({ as: Component = "div", className, onClick, children, ...rest }: ${NAME}Props) => {
const handleClick = (event: React.MouseEvent<HTMLDivElement|Element>) => {
if (onClick) {
onClick(event.currentTarget)
}
}
const classes = classStr(styles.${cssClass}, className)
return (
<Component className={classes} onClick={handleClick} {...rest}>
{children}
</Component>
)
}
import React, { ComponentClass, FunctionComponent } from "react"
export type AnyFunctionComponent = FunctionComponent | ((props: any) => any)
export interface BaseComponentProps {
className?: string
onClick?: (event: any) => void
children?: React.ReactNode
[key: string]: any
}
export interface ExtensibleComponentProps extends BaseComponentProps {
as?: ComponentClass | FunctionComponent | keyof JSX.IntrinsicElements
}
type ClassCondition = boolean | string | undefined | null
type ClassArg = string | undefined | null
type ClassArrArg = ClassArg | [ClassCondition, ClassArg, ClassArg?]
export const classStr = (...args: ClassArrArg[]): string => {
return args
.map((arg) => {
if (Array.isArray(arg)) {
return arg[0] ? arg[1] : arg.length > 2 ? classStr(arg[2]) : undefined
}
return arg
})
// filter(Boolean) filters out falsy values: false, 0, 0, 0n, "", null, undefined, NaN
.filter(Boolean)
.join(" ")
.trim()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment