import React, { useEffect, useRef, useState } from "react";
import WaveSurfer from "wavesurfer.js";
const formWaveSurferOptions = ref => ({
container: ref,
barWidth:1,
waveColor: "#eee",
progressColor: "OrangeRed",
cursorColor: "OrangeRed",
barRadius: 10,
responsive: true,
height: 200,
barGap:0,
pixelRatio: 5,
barMinHeight:100,
normalize: true,
partialRender: true
});
export default function Waveform({ url }) {
const waveformRef = useRef(null);
const wavesurfer = useRef(null);
const [playing, setPlay] = useState(false);
const [volume, setVolume] = useState(0.5);
useEffect(() => {
setPlay(false);
const options = formWaveSurferOptions(waveformRef.current);
wavesurfer.current = WaveSurfer.create(options);
wavesurfer.current.load(url);
wavesurfer.current.on("ready", function() {
if (wavesurfer.current) {
wavesurfer.current.setVolume(volume);
setVolume(volume);
}
});
return () => wavesurfer.current.destroy();
}, [url]);
const handlePlayPause = () => {
...
};
const onVolumeChange = e => {
...
};
return (
<div>
<div id="waveform" ref={waveformRef} />
<div className="controls">
<button onClick={handlePlayPause}>{!playing ? "Play" : "Pause"}</button>
<input
...
/>
<label htmlFor="volume">Volume</label>
</div>
</div>
);
}
The problem in this is that, i want to set the height of the minpeak of the wave and also give the height an average that at this level it does not go up. i tried the minHeightBar, but unfortunatelydoes't work at all, I have connected the two images for better understanding, so if any one knows this so please help, thankyou :)
Related
height and width of a div container in react when I am resizing it using CSS resize property
I have tried this but i am getting error
import React, { useState, useRef, useEffect } from 'react';
const TextAreaWrapper = () => {
const [dimensions, setDimensions] = useState({ height: 0, width: 0 });
const textareaRef = useRef(null);
useEffect(() => {
const textarea = textareaRef.current;
const handleResize = () => {
console.log('Hello world');
setDimensions({
height: textarea.offsetHeight,
width: textarea.offsetWidth,
});
};
textarea.addEventListener('resize', handleResize);
return () => {
textarea.removeEventListener('resize', handleResize);
};
}, []);
return (
<div>
<textarea ref={textareaRef} />
<p>Height: {dimensions.height}</p>
<p>Width: {dimensions.width}</p>
</div>
);
};
export default TextAreaWrapper;
Try this (check that the ref is not null):
useEffect(() => {
const textarea = textareaRef.current;
if (!textarea) {
return;
}
const handleResize = () => {
setDimensions({
height: textarea.offsetHeight,
width: textarea.offsetWidth,
});
};
textarea.addEventListener('resize', handleResize);
return () => {
textarea.removeEventListener('resize', handleResize);
};
}, [textareaRef.current]);
I have an audio element that is being used in multiple other components to play songs. I would like to integrate wavesurfer to this already existing audio. This issue https://github.com/katspaugh/wavesurfer.js/issues/986 says I can load the audio tag, but I get errors when doing it. I believe it's because my audio component is actually of type ForwardRefExoticComponent<Props & RefAttributes<HTMLAudioElement>> instead of just an HTMLAudioElement
const WavesurferComponent = (props: Props) => {
const { isPlaying } = props;
const [waver, setWaver] = useState<MODWaveSurfer | null>(null);
const waveformRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!waveformRef.current) return;
const modedWaveSurfer = WaveSurfer as unknown as MODWaveSurfer;
const wavesurfer = modedWaveSurfer.create({
barWidth: 3,
container: waveformRef.current,
backend: 'WebAudio',
height: 30,
barRadius: 3,
responsive: true,
progressColor: ['#26D1A8', '#AC4EFD', '#F1419E', '#FED503', '#FE5540'],
waveColor: '#1C1B1B',
barGap: 3,
cursorColor: 'transparent',
});
wavesurfer.load(AudioElement);
setWaver(wavesurfer);
return () => wavesurfer.destroy();
}, []);
useEffect(() => {
if (isPlaying && waver) waver.playPause();
}, [isPlaying]);
return (
<div>
<div ref={waveformRef} style={{ width: '225px', margin: '0 auto' }} />
</div>
);
};
// AUDIO ELEMENT
export const AudioElement = forwardRef<HTMLAudioElement, Props>((props, ref) => {
const { className } = props;
return <audio ref={ref} className={className} />;
});
There is a Main component, which has 4 separate components. It is necessary that these components are not visible before the user does not use the search.
The first component is responsible for displaying the weather graph, and the second for displaying the map. I do not know how to hide these two components specifically.
first component 1
import React, { useContext, useState, useEffect } from 'react';
import Chart from 'react-apexcharts';
import { Context } from '../../contex';
import './weather-graph.scss';
import { useTranslation } from 'react-i18next';
const WeatherGrapth = () => {
const { t } = useTranslation()
const {dailyForecast} = useContext(Context);
const [category, setCategory] = useState([])
const [data, setData] = useState([])
useEffect(() => {
const day = [];
const temp =[];
dailyForecast.forEach((d) => {
const unixTimestamp = d.dt;
const getTemp = Math.round(d.temp.day)
let getDay = new Date(unixTimestamp * 1000).getDate();
day.push(getDay)
temp.push(getTemp)
})
setCategory(day)
setData(temp)
}, [dailyForecast]);
return(
<>
{dailyForecast.temp &&
<div className="graph__container">
<h3 className="graph__title">{t("weekly_foreacst")}</h3>
<Chart options={{
chart: {
id: 'weather-graph'
},
xaxis: {
categories: category,
title: {
text: [t("date")],
},
},
yaxis: {
title: {
text: [t("temperature")],
},
},
}}
series={[{
name: 'temp',
data: data
}]} type="line" height={'349px'} />
</div>
}
</>
)
}
export default WeatherGrapth;
second component 2
import React, { useEffect } from 'react';
import './weather-map.scss';
import {API_KEY} from './../../apis/config';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import 'leaflet-openweathermap/leaflet-openweathermap.css';
import 'leaflet-openweathermap';
import { useTranslation } from 'react-i18next';
const WeatherMap = () => {
const { t } = useTranslation();
useEffect(() => {
const osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18, attribution: 'copyright OpenStreetMap contributors' });
const clouds = L.OWM.clouds({showLegend: false, opacity: 0.5, appId: `${API_KEY}`});
const cloudscls = L.OWM.cloudsClassic({showLegend: false,appId: `${API_KEY}`});
const precipitation = L.OWM.precipitation({showLegend: false, appId: `${API_KEY}`});
const precipitationcls = L.OWM.precipitationClassic({showLegend: false,appId: `${API_KEY}`});
const rain = L.OWM.rain({showLegend: false,appId: `${API_KEY}`});
const raincls = L.OWM.rainClassic({showLegend: false,appId: `${API_KEY}`});
const snow = L.OWM.snow({showLegend: false,appId: `${API_KEY}`});
const pressure = L.OWM.pressure({showLegend: false,appId: `${API_KEY}`});
const pressurecntr = L.OWM.pressureContour({showLegend: false,appId: `${API_KEY}`});
const temp = L.OWM.temperature({showLegend: false,appId: `${API_KEY}`});
const wind = L.OWM.wind({showLegend: false,appId: `${API_KEY}`});
const map = L.map('map', { center: new L.LatLng(53.9, 27.5667), zoom: 5, layers: [osm] });
const baseMaps = { "OSM Standard": osm };
const overlayMaps = {
[t("clouds")]: clouds,
[t('cloudscls')]: cloudscls,
[t('precipitation')]: precipitation,
[t('precipitationcls')]: precipitationcls,
[t('rain')]: rain,
[t('raincls')]: raincls,
[t('snow')]: snow,
[t('pressure')]: pressure,
[t('pressurecntr')]: pressurecntr,
[t('temp')]: temp,
[t('wind')]: wind,
};
const layerControl = L.control.layers(baseMaps, overlayMaps,{collapsed:window.innerWidth < 768}).addTo(map);
}, []);
return(
<div className="weathermap-container">
<div id="map" style={{height: '260pt', borderRadius:'20px'}} className="map-weather"></div>
</div>
)
}
export default WeatherMap;
You can achieve that by passing down a prop
For instance
return (
<>
<div className="main-container">
{prop.visible ?
<CardWeather />
<Forecast/>
<WeatherGrapth/>
<WeatherMap/>
: ""
}
</div>
<div className="pr">weather app</div>
</>
)
}
export default Main;```
So to make it visible just pass in
```visible={true} ```
when calling the function
Basically, I want to add cropping functionality. If user select a file then, user have choice to crop the image if he/she want. When I preview cropped image.
Error: Invalid hook call. Hooks can only be called inside of the body
of a function component. This could happen for one of the following
reasons: 1. You might have mismatching versions of React and the
renderer (such as React DOM) 2. You might be breaking the Rules of
Hooks 3. You might have more than one copy of React in the same app
import React, { useEffect, useState, useRef } from 'react';
import ReactCrop from 'react-image-crop';
import 'react-image-crop/dist/ReactCrop.css';
// import Styles from './Image.module.css';
const Image = (props) => {
const [crop, setCrop] = useState({
aspect: 3/4,
unit: 'px',
x: 0,
y: 0,
width: 500,
height: 500
});
const [file, setFile] = useState(null);
const [imgPreview, setImgPreview] = useState(null);
const canvasRef = useRef(null);
const filePicker = (e) => {
setFile(e.target.files[0]);
};
function image64toCanvasRef (cnvRef, image64, pixelCrop) {
const canvas = cnvRef;
canvas.width = pixelCrop.width;
canvas.height = pixelCrop.height;
const ctx = canvas.getContext('2d');
const image = new Image(); // On this line throwing error
image.src = image64
image.onload = () => {
ctx.drawImage(
image,
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height,
0,
0,
pixelCrop.width,
pixelCrop.height
)
}
}
useEffect(() => {
if (file) {
const fileReader = new FileReader();
fileReader.onload = () => {
setImgPreview(fileReader.result);
}
fileReader.readAsDataURL(file);
}
}, [file]);
const handleOnCropChanged = (crop) => {
// console.log('handleOnCropChanged: ', crop);
const state = {
...crop,
x: crop.x,
y: crop.y,
width: crop.width,
height: crop.height
}
setCrop(state);
};
const handleOnCropComplete = (crop, pixelCrop) => {
image64toCanvasRef(canvasRef.current, imgPreview, pixelCrop);
}
return (
<div
style={{
margin: '10px 28px',
}}
>
{
imgPreview ? (
<div>
<ReactCrop
src={imgPreview}
crop={crop}
keepSelection
locked
onChange={(crop) => handleOnCropChanged(crop)}
onComplete={handleOnCropComplete}
onImageLoaded={handleOnImageLoaded}
/>
</div>
) : (
<input type='file' onChange={filePicker} />
)
}
<div>
<canvas
ref={canvasRef}
></canvas>
</div>
</div>
)
};
export default Image;
Dears
I am working on React project and currently struggling with small problem.
what is an issue ? When app is loading on a mobile view I see that all components are showing and after few ms unneeded data disappear. I would like to modify my app to display only needed components.
import React, { useState, useEffect } from "react";
import styled from "styled-components";
import PropTypes from "prop-types";
import RegistrationInputSide from "./../RegistrationInputSide/RegistrationInputSide";
import AppInfoSide from "./../AppInfoSide/AppInfoSide";
import SuccessWindow from "./../SuccessWindow/SuccessWindow";
import { names, labels, types} from "./registrationFormData";
const FormStyle = styled.div.attrs(({ className }) => ({
className,
}))`
display: flex;
width: 100%;
height: 100%;
.fields {
display: ${({ fieldsVisibility }) => (fieldsVisibility ? "flex" : "none")};
}
.info {
display: ${({ infoVisibility }) => (infoVisibility ? "flex" : "none")};
}
`;
const RegistrationForm = ({user, onChange, validation, onSubmit, formCorrectness, formModified}) => {
const [isMobile, setIsMobile] = useState(false);
const [isMoved, setIsMoved] = useState(false);
const [firstRender, setFirstRender] = useState(true);
const {password, repeatedPassword, email } = user;
const {emailValidation, passwordValidation, repeatedPasswordValidation} = validation;
const calculateMobileVisibility = (form) => {
const {name, isMobile} = form;
const visibilityRequirements = {
"info": [{"isMobile":true, "isMoved":false, "isModified":false, "formCorrectness": false}],
"fields": [{"isMobile":true, "isMoved":true, "isModified":false, "formCorrectness": false},{"isMobile":true, "isMoved":false, "isModified":true, "formCorrectness": false}, {"isMobile":true, "isMoved":true, "isModified":true, "formCorrectness": false}],
"success": [{"isMobile":true, "isMoved":true, "isModified":false, "formCorrectness": true},{"isMobile":true, "isMoved":false, "isModified":true, "formCorrectness": true}, {"isMobile":true, "isMoved":true, "isModified":true, "formCorrectness": true}]
}
if(isMobile === false){
return true;
}
const requirements = visibilityRequirements[name];
delete form.name;
let resultArr = requirements.map(requirement => {return Object.keys(form).map(formItem => requirement[formItem] === form[formItem])});
const finalArr = resultArr.map(singleRes => singleRes.every((val) => val === true));
return finalArr.includes(true);
};
const calculateInfoSideVisiblity = () => {
return calculateMobileVisibility({"isMobile":isMobile, "isMoved": isMoved, "isModified": formModified ,"formCorrectness": formCorrectness, "name": "info"});
}
const calculateFieldsSideVisibility = () => {
return calculateMobileVisibility({"isMobile":isMobile, "isMoved": isMoved, "isModified": formModified ,"formCorrectness": formCorrectness, "name": "fields"});
}
const onClickHandleMobile = (e) => {
e.preventDefault();
setIsMoved(true);
};
const handleResize = () => {
window.visualViewport.width > 576 ? setIsMobile(false) : setIsMobile(true);
};
useEffect(() => {
if(firstRender===true){
handleResize();
setFirstRender(false);
}
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
};
});
return (
<FormStyle fieldsVisibility={calculateFieldsSideVisibility()} infoVisibility={calculateInfoSideVisiblity()}>
<form onSubmit={onSubmit}>
<AppInfoSide
className="form-info-side info"
onClick={onClickHandleMobile}
/>
{!formCorrectness && (<RegistrationInputSide
className="form-user-input-side fields"
inputFieldsData={inputFieldsData}
onClick={onSubmit}
onChange={onChange}
user={user}
/>)}
{formCorrectness && (<SuccessWindow
className="success-window fields"
successMessage="Account successfully created!"
/>)}
</form>
</FormStyle>
);
};
I do not know how to prevent displaying unwanted elements.