Skip to content

Instantly share code, notes, and snippets.

@isabellachen
isabellachen / MarkdownEditor.tsx
Created March 13, 2020 14:19
sidra draftToMarkdown custom style handler
import { convertFromRaw, convertToRaw, Editor, EditorState, RichUtils } from 'draft-js';
import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js';
import React, { useEffect, useState } from 'react';
import { useIsFirstRender } from '~/core/utils';
import './markdown-editor.scss';
import { BlockTypeControls } from './MarkdownEditorBlockTypeControls';
import { InlineStyleControls } from './MarkdownEditorInlineStyleControls';
type MarkdownEditorProps = {
markdown: string;
@isabellachen
isabellachen / useChromaBackground.ts
Last active March 10, 2020 10:45
useChromeBackground hook determines background-color of DOM element and sets the the color for adequate contrast
import chroma from 'chroma-js';
import { MutableRefObject, useEffect, useRef } from 'react';
export const useChromaBackground = (): MutableRefObject<HTMLElement> => {
const tagRef = useRef<HTMLElement>({} as HTMLElement);
const cWhite = '#FFFFFF';
const minClarityValue = 70;
useEffect(() => {
async function validateBackground() {
@isabellachen
isabellachen / load-react-into-wp-theme.md
Created February 29, 2020 17:04
Load react into wp theme

Loading React into WP

Create a child theme, or if you want to load React into a plugin, you can do this in a plugin.

Make sure you have node and npm installed globally in your machine.

Make sure you have some way to access the command line.

npm init in the child theme to enable it to use npm packages. You now see a package.json file in your child theme directory. Listed here are the libraries your application depends on. It's a bit like wp_enqueue_scripts, but for JS libraries. All the external libraries/scripts you need are listed under devDependencies or dependencies.

@isabellachen
isabellachen / shortcode-create-story.js
Created February 22, 2020 14:05
Create a Post with fetch in Wordpress with REST API and authentication with nonces
(function($) {
const postData = {
title: 'Lorem ipsum dolor sit amet',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
status: 'publish'
};
window
.fetch(`${scriptVars.endpoint}`, {
method: 'POST',
@isabellachen
isabellachen / generic-component-props.ts
Created February 21, 2020 14:03
generic component props
import { PropsWithChildren } from 'react';
export type GenericComponentProps = PropsWithChildren<Record<string, any>>;
const MetaBox = (props: GenericComponentProps) => (
<div className="metainfo__box">
<div>{t(`entityDetail.${props.label}`)}</div>
<div>{props.children}</div>
</div>
);
@isabellachen
isabellachen / AsideLink.tsx
Last active February 17, 2020 10:54
Nested Menu with React
@isabellachen
isabellachen / basic-animated-hero.md
Created February 1, 2020 16:25
Creating an animated hero carousel

#Basic animated hero

We begin by adding one image and animating it. We start with some basic markup to scaffold the project and show the elements of the header. We will animate the header__image-container and add styles to the img element to ensure correct sizing and placement. We add a ::before psuedo element to the header__image-container to darken the image a little.

<!DOCTYPE html>
<html>
@isabellachen
isabellachen / react-simple-maps-marker-react-tooltip.md
Last active January 31, 2020 13:03
React simple maps with react tooltip on a marker
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
  ComposableMap,
  Geographies,
  Geography,
  Marker,
  ZoomableGroup,
} from 'react-simple-maps';
@isabellachen
isabellachen / clickable-overlay-close-modal.md
Created January 27, 2020 14:53
CSS to create clikable overlay

The idea is to have an overlay on the same level as the modal or widget, so when click the overlay, the modal closes.

<div class="overlay"></div>
<div class="widget"></div>
.widget {
 width: 50px;