-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Typescript support, export only components #21
Open
czue
wants to merge
13
commits into
main
Choose a base branch
from
skoch-typescript-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6830248
add TS support, component
skoch 25ab7a1
remove .DS_Store file
skoch 7b2982d
update README installation instructions
czue c856c72
update usage section
czue 8d70a39
merge two installaiton sections
czue 4b7e80c
simplify and fix extra div
czue ce9c237
clearer headings
czue 632296c
remove react-dom
czue 1acb7a7
fix closing tag
czue 8487bbf
clarify react/jsx language
czue 99ebc3f
restore legacy instructions
czue e00857b
update package for types, add css injection
skoch 5bb1fd2
Merge pull request #22 from skoch/typescript-component
czue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,39 @@ Embed Bluesky comments on your website easily. | |
|
||
**[Write up and demo here](https://coryzue.com/writing/bluesky-comments).** | ||
|
||
## Installation in a Node.js project as a React component | ||
|
||
## Installation via CDNs (easiest) | ||
To use this library in a React project, first install the library: | ||
|
||
There are a few ways to set up the library on your website. | ||
```bash | ||
npm install bluesky-comments | ||
``` | ||
|
||
Then import it (and the CSS) in your React app/page/component: | ||
|
||
```tsx | ||
import 'bluesky-comments/bluesky-comments.css' | ||
import { BlueskyComments } from 'bluesky-comments'; | ||
``` | ||
|
||
And use it in any React component like this: | ||
|
||
```javascript | ||
function App() { | ||
return ( | ||
<> | ||
<div>Comments Will Display Below</div> | ||
<BlueskyComments author="you.bsky.social" /> | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
See the [Usage](#usage) section below for details on the options and API. | ||
|
||
## Installation on any website via CDN | ||
|
||
To add a comments section to any website, follow these steps | ||
|
||
### 1. Add an element to your page where you want the comments to show up | ||
|
||
|
@@ -17,9 +46,8 @@ Add something like this to your site: | |
<div id="bluesky-comments"></div> | ||
``` | ||
|
||
You can use whatever id you want, but it has to match the value used in `BlueskyComments.init` | ||
in the later steps. | ||
|
||
You can use whatever id you want, but it has to match the container id used in the `getElementById` call | ||
in the usage step. | ||
|
||
### 2. Add the CSS files | ||
|
||
|
@@ -38,64 +66,66 @@ Add the following importmap to your page anywhere before you use the library: | |
{ | ||
"imports": { | ||
"react": "https://esm.sh/react@18", | ||
"react-dom": "https://esm.sh/react-dom@18" | ||
"react-dom/client": "https://esm.sh/react-dom@18/client" | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
### 4. Import and use the library and any other functions you need in an ES module script: | ||
### 4. Import the library and instantiate the component with React in an ES module script: | ||
|
||
```html | ||
<script type="module"> | ||
import { BlueskyComments } from 'https://unpkg.com/[email protected]/dist/bluesky-comments.es.js'; | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const author = 'you.bsky.social'; | ||
if (author) { | ||
BlueskyComments.init('bluesky-comments', {author}); | ||
} | ||
}); | ||
import { createElement } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { BlueskyComments } from 'https://unpkg.com/bluesky-comments@<VERSION>/dist/bluesky-comments.es.js'; | ||
|
||
const author = 'you.bsky.social'; | ||
const container = document.getElementById('bluesky-comments'); | ||
const root = createRoot(container); | ||
root.render( | ||
createElement(BlueskyComments, { | ||
"author": author, | ||
}) | ||
); | ||
</script> | ||
``` | ||
|
||
See the [Usage](#usage) section for details on the API. | ||
See the [Usage](#usage) section below for details on the options and API. | ||
|
||
### (Deprecated) Installation using `<script>` tags and UMD | ||
## Usage | ||
|
||
Previous versions of this library recommended installing like this: | ||
Examples in this section use the React JSX syntax. If you're installing on a project that doens't | ||
use JSX or any build tooling (i.e. a regular website), you can instead use the `createElement` | ||
function and pass the react options in. | ||
|
||
```html | ||
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script> | ||
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script> | ||
<script src="https://unpkg.com/bluesky-comments@<VERSION>/dist/bluesky-comments.umd.js"></script> | ||
``` | ||
For example, the following two examples are equivalent: | ||
|
||
And initializing the comments in a standard `<script>` tag. Both of these approaches work: | ||
React JSX: | ||
|
||
```html | ||
<script> | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const uri = 'https://bsky.social/coryzue.com/posts/3jxgux'; | ||
if (uri) { | ||
// New API | ||
BlueskyComments.init('bluesky-comments', {uri}); | ||
|
||
// Legacy API (still supported but deprecated) | ||
initBlueskyComments('bluesky-comments', {uri}); | ||
} | ||
}); | ||
</script> | ||
```javascript | ||
<BlueskyComments | ||
author="you.bsky.social" | ||
uri="https://bsky.app/profile/coryzue.com/post/3lbrko5zsgk24" | ||
/> | ||
``` | ||
|
||
This option is now deprecated with the introduction of ES modules and will be removed in a future version. | ||
Equivalent without JSX: | ||
|
||
## Usage | ||
```javascript | ||
root.render( | ||
createElement(BlueskyComments, { | ||
author: "you.bsky.social", | ||
uri: "https://bsky.app/profile/coryzue.com/post/3lbrko5zsgk24", | ||
}) | ||
); | ||
``` | ||
|
||
### Initializing the library based on the author | ||
|
||
|
||
```javascript | ||
const author = 'you.bsky.social'; | ||
BlueskyComments.init('bluesky-comments', {author}); | ||
<BlueskyComments author="you.bsky.social" /> | ||
``` | ||
|
||
If you use this mode, the comments section will use the most popular post by that author that links | ||
|
@@ -104,28 +134,28 @@ to the current page. | |
### Initializing the library based on a post URL | ||
|
||
```javascript | ||
const uri = 'https://bsky.social/coryzue.com/posts/3jxgux'; | ||
BlueskyComments.init('bluesky-comments', {uri}); | ||
<BlueskyComments uri="https://bsky.app/profile/coryzue.com/post/3lbrko5zsgk24" /> | ||
``` | ||
|
||
If you use this mode, the comments section will use the exact post you specify. | ||
This usually means you have to add the comments section only *after* you've linked to the article. | ||
|
||
|
||
### (Advanced) Providing custom default empty states | ||
|
||
You can pass in a `onEmpty` callback to handle the case where there are no comments rendered | ||
(for example, if no post matching the URL is found or there aren't any comments on it yet): | ||
|
||
```javascript | ||
BlueskyComments.init('bluesky-comments', { | ||
uri, | ||
author, | ||
onEmpty: (details) => { | ||
console.error('Failed to load comments:', details); | ||
document.getElementById('bluesky-comments').innerHTML = | ||
'No comments on this post yet. Details: ' + details.message; | ||
}, | ||
<BlueskyComments | ||
uri="https://bsky.app/profile/coryzue.com/post/3lbrko5zsgk24" | ||
author="you.bsky.social" | ||
onEmpty={ | ||
(details) => { | ||
console.error('Failed to load comments:', details); | ||
document.getElementById('bluesky-comments').innerHTML = | ||
'No comments on this post yet. Details: ' + details.message; | ||
} | ||
} | ||
}); | ||
``` | ||
|
||
|
@@ -135,26 +165,28 @@ You can pass in an array of filters to the `commentFilters` option. These are fu | |
|
||
A few default filters utilities are provided: | ||
|
||
- `BlueskyComments.Filters.NoPins`: Hide comments that are just "📌" | ||
- `BlueskyComments.Filters.NoLikes`: Hide comments with no likes | ||
- `BlueskyFilters.NoPins`: Hide comments that are just "📌" | ||
- `BlueskyFilters.NoLikes`: Hide comments with no likes | ||
|
||
You can also use the following utilities to create your own filters: | ||
|
||
- `BlueskyComments.Filters.MinLikeCountFilter`: Hide comments with less than a given number of likes | ||
- `BlueskyComments.Filters.MinCharacterCountFilter`: Hide comments with less than a given number of characters | ||
- `BlueskyComments.Filters.TextContainsFilter`: Hide comments that contain specific text (case insensitive) | ||
- `BlueskyComments.Filters.ExactMatchFilter`: Hide comments that match text exactly (case insensitive) | ||
- `BlueskyFilters.MinLikeCountFilter`: Hide comments with less than a given number of likes | ||
- `BlueskyFilters.MinCharacterCountFilter`: Hide comments with less than a given number of characters | ||
- `BlueskyFilters.TextContainsFilter`: Hide comments that contain specific text (case insensitive) | ||
- `BlueskyFilters.ExactMatchFilter`: Hide comments that match text exactly (case insensitive) | ||
|
||
Pass filters using the `commentFilters` option: | ||
|
||
```javascript | ||
BlueskyComments.init('bluesky-comments', { | ||
import {BlueskyComments, BlueskyFilters} from 'bluesky-comments'; | ||
|
||
<BlueskyComments | ||
// other options here | ||
commentFilters: [ | ||
BlueskyComments.Filters.NoPins, // Hide pinned comments | ||
BlueskyComments.Filters.MinCharacterCountFilter(10), // Hide comments with less than 10 characters | ||
], | ||
}); | ||
commentFilters={[ | ||
BlueskyFilters.NoPins, // Hide pinned comments | ||
BlueskyFilters.MinCharacterCountFilter(10), // Hide comments with less than 10 characters | ||
]} | ||
/> | ||
``` | ||
|
||
You can also write your own filters, by returning `true` for comments you want to hide: | ||
|
@@ -163,49 +195,41 @@ You can also write your own filters, by returning `true` for comments you want t | |
const NoTwitterLinksFilter = (comment) => { | ||
return (comment.post.record.text.includes('https://x.com/') || comment.post.record.text.includes('https://twitter.com/')); | ||
} | ||
BlueskyComments.init('bluesky-comments', { | ||
<BlueskyComments | ||
// other options here | ||
commentFilters: [ | ||
commentFilters={[ | ||
NoTwitterLinksFilter, | ||
] | ||
}); | ||
/> | ||
``` | ||
|
||
## Usage with npm / yarn in a native JavaScript project | ||
### (Removed) Legacy installation using `<script>` tags and UMD | ||
|
||
Install the package: | ||
Previous versions of this library recommended installing like this: | ||
|
||
```bash | ||
npm install bluesky-comments | ||
```html | ||
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script> | ||
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script> | ||
<script src="https://unpkg.com/bluesky-comments@<VERSION>/dist/bluesky-comments.umd.js"></script> | ||
``` | ||
|
||
Then you can use the library in your projects by importing the CSS and components: | ||
|
||
```javascript | ||
import 'bluesky-comments/bluesky-comments.css' | ||
import { CommentSection } from "bluesky-comments"; | ||
``` | ||
And initializing the comments in a standard `<script>` tag with an `init` function: | ||
|
||
And using them in a React component like this: | ||
```html | ||
<script> | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const uri = 'https://bsky.social/coryzue.com/posts/3jxgux'; | ||
if (uri) { | ||
BlueskyComments.init('bluesky-comments', {uri}); | ||
|
||
```javascript | ||
function App() { | ||
return ( | ||
<> | ||
<div>Comments Will Display Below</div> | ||
<CommentSection | ||
author="coryzue.com" | ||
uri="" | ||
onEmpty={() => <div>No comments yet</div>} | ||
commentFilters={[]} /> | ||
</div> | ||
</> | ||
) | ||
} | ||
// Legacy API (still supported but deprecated) | ||
initBlueskyComments('bluesky-comments', {uri}); | ||
} | ||
}); | ||
</script> | ||
``` | ||
|
||
|
||
I don't publish a lot of JavaScript packages, but I think this should work! | ||
This option has been removed in version 0.9.0 and new projects should use the ES module syntax above. | ||
|
||
|
||
## Development | ||
|
@@ -214,9 +238,12 @@ To develop on this package, you can run: | |
|
||
``` | ||
npm install | ||
npm run watch | ||
npm run dev | ||
``` | ||
|
||
This will watch for changes and copy the built files to the `dist` directory. | ||
From there you can reference the files in your own project and any updates you make | ||
should show up instantly. | ||
This will set up a local development server with a simple page showing comments, | ||
and watch for changes. | ||
|
||
You can also run `npm run build` (build once) or `npm run watch` (watch for changes) | ||
to copy the built files to the `dist` directory. | ||
From there you can reference the files in your own projects. |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Bluesky Comments Component Test</title> | ||
<style> | ||
body { | ||
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', | ||
Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', | ||
sans-serif; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/index.tsx"></script> | ||
</body> | ||
</html> |
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,21 @@ | ||
import React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import { BlueskyComments, BlueskyFilters } from '../src/main'; | ||
|
||
const App = () => ( | ||
<div> | ||
<h1>Testing Bluesky Comments Component</h1> | ||
<BlueskyComments | ||
uri="https://bsky.app/profile/coryzue.com/post/3lbrko5zsgk24" | ||
commentFilters={[ | ||
BlueskyFilters.NoPins, // Hide pinned comments | ||
BlueskyFilters.MinCharacterCountFilter(10), // Hide comments with less than 10 characters | ||
]} | ||
/> | ||
</div> | ||
); | ||
|
||
const container = document.getElementById('app'); | ||
const root = createRoot(container!); | ||
root.render(<App />); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to import the CSS, the component is handling that behind the scenes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found (in my test vite app at least) that I still had to do this. If that's not expected, any idea under what circumstances that might be true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If using in a React app this shouldn't be needed. Take a look at the
index.tsx
file in/dev
-- no need to import the css since the component itself is importing.If you are using this in the non JSX way, then yes, you will need to import the CSS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, maybe an issue with my dev setup then? What I did:
npm run build
If I leave that css import commented out the styles don't work, but if I add it they do.