Skip to content

Instantly share code, notes, and snippets.

Avatar
:electron:
JavaScripting

Mahdi Yazdani mahdiyazdani

:electron:
JavaScripting
View GitHub Profile
View use-window-dimensions-react-hook.js
import React from 'react';
import ReactDOM from 'react-dom';
function useWindowDimensions() {
const [width, setWidth] = React.useState(window.innerWidth);
const [height, setHeight] = React.useState(window.innerHeight);
React.useEffect(() => {
const handleResize = () => {
setWidth(window.innerWidth);
View use-wait-react-hook.js
import React from 'react';
import ReactDOM from 'react-dom';
function useWait(delay) {
const [status, setStatus] = React.useState(false);
React.useEffect(() => {
const id = window.setTimeout(() => {
setStatus((c) => !c);
}, delay);
@mahdiyazdani
mahdiyazdani / remove-add-to-cart-variable-products.css
Created Dec 20, 2020
Remove add to cart button from variable products when "Store Vacation PRO" is activated
View remove-add-to-cart-variable-products.css
.wsvpro + .product form.cart {
display: none;
}
@mahdiyazdani
mahdiyazdani / fetch-data-with-useeffect.js
Created Nov 27, 2020
Fetch and display data with `useEffect`
View fetch-data-with-useeffect.js
// 1. Destructure the `subreddit` from props:
function Reddit({ subreddit }) {
const [posts, setPosts] = useState([]);
useEffect(() => {
async function fetchData() {
// 2. Use a template string to set the URL:
const res = await fetch(
`https://www.reddit.com/r/${subreddit}.json`
);
@mahdiyazdani
mahdiyazdani / render-once-useeffect.js
Created Nov 27, 2020
Focus an input control upon first render, using `useEffect` combined with the `useRef` hook
View render-once-useeffect.js
import React, { useEffect, useState, useRef } from "react";
import ReactDOM from "react-dom";
function App() {
// Store a reference to the input's DOM node
const inputRef = useRef();
const [value, setValue] = useState("");
useEffect(
() => {
@mahdiyazdani
mahdiyazdani / float-to-string.php
Created Nov 1, 2020
Convert a float to a string without locale formatting
View float-to-string.php
<?php
/**
* Convert a float to a string without locale formatting which PHP adds when changing floats to strings.
*
* @param float $float Float value to format.
* @return string
*/
function prefix_float_to_string( $float ) {
if ( ! is_float( $float ) ) {
@mahdiyazdani
mahdiyazdani / string-to-array.php
Created Nov 1, 2020
Explode a string into an array by delimiter
View string-to-array.php
<?php
/**
* Explode a string into an array by $delimiter and remove empty values.
*
* @param string $string String to convert.
* @param string $delimiter Delimiter, defaults to ','.
* @return array
*/
function prefix_string_to_array( $string, $delimiter = ',' ) {
@mahdiyazdani
mahdiyazdani / wpml-config.xml
Created Nov 1, 2020
WPML language configuration file
View wpml-config.xml
<wpml-config>
<custom-fields>
<custom-field action="copy">quantity</custom-field>
<custom-field action="translate">custom-title</custom-field>
</custom-fields>
<custom-types>
<custom-type translate="1">book</custom-type>
<custom-type translate="1">DVD</custom-type>
</custom-types>
<taxonomies>
@mahdiyazdani
mahdiyazdani / string-to-bool.php
Created Oct 26, 2020
Converting a string (e.g. 'yes' or 'no') to a boolean
View string-to-bool.php
<?php
/**
* Converts a string (e.g. 'yes' or 'no') to a bool.
*
* @since 1.0.0
* @param string $input String to convert.
* @return bool
*/
function prefix_string_to_bool( $input ) {
return is_bool( $input ) ? $input : ( 'yes' === $input || 1 === $input || 'true' === $input || 'TRUE' === $input || '1' === $input );
@mahdiyazdani
mahdiyazdani / get-next-array-key.php
Created Oct 26, 2020
Retrieve the next key in an array from given key
View get-next-array-key.php
<?php
/**
* Retrieve the next key in an array from given key.
*
* @since 1.0.0
* @param array $arr List of items in a form of array.
* @param integer $key Key to search and find within the array.
* @return integer
*/