-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'update/vendor-dashboard-structure' into enhance/add-rea…
…ct-table-filter-component # Conflicts: # docs/frontend/components.md # includes/Assets.php # src/components/dataviews/DataViewTable.tsx # src/components/index.tsx # webpack.config.js
- Loading branch information
Showing
10 changed files
with
201 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Dokan Hooks | ||
|
||
## Overview | ||
|
||
`Dokan` provides a set of reusable `hooks` that can be used across both `Free` and `Pro` versions. This documentation explains how to properly set up and use `hooks` in your project. | ||
|
||
## Important Dependencies | ||
|
||
For both `Dokan Free and Pro` versions, we must register the `dokan-react-components` dependency when using `global` components. | ||
|
||
### Implementation Example | ||
|
||
```php | ||
// Register scripts with dokan-react-components dependency | ||
$script_assets = 'add_your_script_assets_path_here'; | ||
|
||
if (file_exists($script_assets)) { | ||
$vendor_asset = require $script_assets; | ||
$version = $vendor_asset['version'] ?? ''; | ||
|
||
// Add dokan-react-components as a dependency | ||
$component_handle = 'dokan-react-components'; | ||
$dependencies = $vendor_asset['dependencies'] ?? []; | ||
$dependencies[] = $component_handle; | ||
|
||
// Register Script | ||
wp_register_script( | ||
'handler-name', | ||
'path_to_your_script_file', | ||
$dependencies, | ||
$version, | ||
true | ||
); | ||
|
||
// Register Style | ||
wp_register_style( | ||
'handler-name', | ||
'path_to_your_style_file', | ||
[ $component_handle ], | ||
$version | ||
); | ||
} | ||
``` | ||
|
||
## Component Access | ||
|
||
For `Dokan Free`, we can import the components via `@/hooks`: | ||
|
||
```js | ||
import { useWindowDimensions } from '@/hooks'; | ||
``` | ||
|
||
In `Dokan Pro`, components can be imported directly from `@dokan/components`: | ||
|
||
```js | ||
import { useWindowDimensions } from '@dokan/hooks'; | ||
``` | ||
|
||
For external `plugins`, we must include the `dokan-react-components` as scripts dependency and the `@dokan/hooks` should be introduced as an external resource configuration to resolve the path via `webpack`: | ||
|
||
```js | ||
externals: { | ||
'@dokan/hooks': 'dokan.hooks', | ||
... | ||
}, | ||
``` | ||
|
||
## Adding Global Components | ||
|
||
### File Structure | ||
|
||
``` | ||
|____ src/ | ||
| |___ hooks/ | ||
| | |___ index.tsx # Main export file | ||
| | |___ ViewportDimensions.tsx # Existing hook | ||
| | |___ YourHook # Your new hook | ||
| | | | ||
| | |___ Other Files | ||
| | | ||
| |___ Other Files | ||
| | ||
|____ Other Files | ||
``` | ||
|
||
**Finally,** we need to export the new `hook` from the `src/hooks/index.tsx` file. Then, we can import the new component from `@dokan/hooks` in `dokan pro` version. | ||
|
||
```tsx | ||
export { default as useWindowDimensions } from '@/hooks/ViewportDimensions'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState, useEffect, useCallback } from '@wordpress/element'; | ||
|
||
interface ViewportDimensions { | ||
width: number | null; | ||
height: number | null; | ||
} | ||
|
||
/** | ||
* Hook to track viewport dimensions. | ||
* | ||
* @since DOKAN_PRO_SINCE | ||
* | ||
* @return {ViewportDimensions} The viewport dimensions. | ||
*/ | ||
export default function useWindowDimensions() { | ||
const getViewportDimensions = useCallback((): ViewportDimensions => ({ | ||
width: typeof window !== 'undefined' ? window.innerWidth : null, | ||
height: typeof window !== 'undefined' ? window.innerHeight : null, | ||
}), []); | ||
|
||
const [viewport, setViewport] = useState<ViewportDimensions>(getViewportDimensions()); | ||
|
||
useEffect(() => { | ||
if (typeof window === 'undefined') { | ||
return; | ||
} | ||
|
||
const handleResize = () => { | ||
// Use requestAnimationFrame to throttle updates | ||
window.requestAnimationFrame(() => { | ||
setViewport(getViewportDimensions()); | ||
}); | ||
}; | ||
|
||
window.addEventListener('resize', handleResize); | ||
|
||
// Initial measurement after mount | ||
handleResize(); | ||
|
||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, [getViewportDimensions]); | ||
|
||
return viewport; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useWindowDimensions } from '@/hooks/ViewportDimensions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters